随笔分类 - python基础
摘要:""" 享元模式 将不可变(可共享)的属性与可变的属性分开 """ import random from enum import Enum TreeType = Enum('TreeType', 'apple_tree cherry_tree peach_tree') class Tree: poo
        阅读全文
                
摘要:""" 修饰器模式 """ import functools def memoize(fn): known = dict() @functools.wraps(fn) def memoizer(*args): if args not in known: known[args] = fn(*args)
        阅读全文
                
摘要:""" 日志模块的使用 """ import logging # 加上filename,日志会输出到文件里 logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(l
        阅读全文
                
摘要:""" 异常信息保存到文件中 """ import traceback try: raise Exception('This is the error message.') except: errorFile = open('errorInfo.txt', 'w') errorFile.write(
        阅读全文
                
摘要:""" 利用 shelve 模块,你可以将 Python 程序中的变量保存到二进制的 shelf 文件中。 这样,程序就可以从硬盘中恢复变量的数据 """ import shelve def save(): # 在 OS X 上,只会创建一个 mydata.db 文件 # 调用函数shelve.op
        阅读全文
                
摘要:helloFile = open('./hello.txt') # 默认是读模式 helloContent = helloFile.read() helloFile.close() print(helloContent) # 以使用 readlines()方法,从该文件取得一个字符串的列表。 # 列
        阅读全文
                
摘要:import os print(os.path.join('usr', 'local', 'bin')) print(os.getcwd()) print(os.path.abspath('.')) print(os.path.abspath('./Os.py')) print(os.path.is
        阅读全文
                
摘要:import pyperclip # 向剪切板写内容 pyperclip.copy('hello world') # 获取剪切板的内容 print(pyperclip.paste())
        阅读全文
                
摘要:import pyttsx3 engine = pyttsx3.init() engine.say('Sally sells seashells by the seashore.') engine.runAndWait() # 必须使用这行代码才能发出语音 engine.say('语音合成开始') 
        阅读全文
                
摘要:在使用httpclient访问接口时得到的数据是以"b’\x1f\x8b\x08"开头的 ,说明它是gzip压缩过的数据,需要对我们接收的字节码进行一个解码操作 from urllib import request from io import BytesIO import gzip def xxx
        阅读全文
                
摘要:""" 官网:https://docs.python-requests.org/zh_CN/latest/ Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用。 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症、冗余代码症、重新发明轮子症、啃文
        阅读全文
                
摘要:使用http.client访问接口 import http.client import urllib.parse import ssl import json header_data = { "Host": "gank.io", "Accept-Encoding": "gzip", "User-Ag
        阅读全文
                
摘要:import numpy as np from matplotlib import pyplot as plt x = np.arange(0, 2 * np.pi, 0.1) # y = np.sin(x) # plt.title("sin(x)") # plt.xlabel("x") # plt
        阅读全文
                
摘要:"""三角函数""" import numpy as np a = np.array([0, 30, 45, 60, 90]) print('不同角度的正弦值:') # 通过乘 pi/180 转化为弧度 print(np.sin(a * np.pi / 180)) print('数组中角度的余弦值:
        阅读全文
                
摘要:import re line = 'asdf fjdk; afed, fjek,asdf, foo' # 切割 print(re.split(r'[;,\s]\s*', line)) # ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo'] # 分组替换 p
        阅读全文
                
摘要:# 链map from collections import ChainMap a = {'x': 1, 'z': 3} b = {'y': 2, 'z': 4} c = ChainMap(a, b) print(c['x']) print(c['y']) print(c['z']) # 3 重复的
        阅读全文
                
摘要:# 命名元组 from collections import namedtuple subscriber = namedtuple(typename='Subscriber', field_names=['name', 'age']) sub = subscriber('lisi', 10) pri
        阅读全文
                
摘要:values = ['1', '2', '-3', '-', '4', 'N/A', '5'] def isNum(s): try: int(s) return True except: return False print(list(filter(lambda i: isNum(i), value
        阅读全文
                
摘要:from operator import itemgetter from itertools import groupby rows = [ {'address': '5412 N CLARK', 'date': '07/01/2012'}, {'address': '5148 N CLARK', 
        阅读全文
                
摘要:from operator import itemgetter from operator import attrgetter rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname'
        阅读全文
                
 
                    
                
 浙公网安备 33010602011771号
浙公网安备 33010602011771号