1、类型判断
data = b'' data = bytes() print (type(data)) #<class 'bytes'> isinstance(123,int)
if type(L) == type([]):
print ("yes")
if type(L) == list:
print ("yes")
if isinstance(L, list):
print ("yes")
if L is M
print ("Same")
if not isinstance(x, list):
tot += x
2、指定编码格式
with open("d:kitkingiot.txt", 'rt',encoding='utf-8') as f: data = f.read() print (data) #二进制读写 with open("d:kitkingiot.bin", 'rb') as f: data = f.read() with open("somefile.bin",'wb') as f: f.write(b'hello')
#如果打开时没有指定编码格式,可以用包装器在打开后指定编码格式 import urllib.request import io u = urllib.request.urlopen('http://www.python.org') f = io.TextIOWrapper(u, encoding='utf-8') text = f.read() print(text)
3、无格式字节流
bytes 不可变字节类型
byrearray 可变字节数组
>>> b = bytearray() >>> b.append(10) >>> b.remove(100) >>> b.insert(0, 150) >>> b.extend([1, 3, 5]) >>> b.pop(2) >>> b.reverse() >>> b.clear() #返回16进制表示的字符串 >>>bytearray(‘abc’.encode()).hex() #bytes<->str转换 >>> aStr = u'abc' >>> b= bytes(aStr, encoding='utf-8') >>> newStr = str(b, encoding='utf-8') #动态开buff buf = bytearray(os.path.getsize(filename)) #读文件到buf with open(fileName, 'rb') as f: f.readinto(buf)
#数值<->字串转换
int("42"),str(42)
4、I/O映射
import io s = io.StringIO() f = io.BytesIO()
5、文件->内存映射(切片方式修改内存)
import mmap fileName = "d:kitkingiotBaidu.bin" size = os.path.getsize(fileName) fd = os.open(fileName, os.O_RDWR) m = mmap.mmap(fd, size, access=mmap.ACCESS_WRITE) m[0:11] = b'Hello World' m.close()
#shell下使用"od -x iotBaidu.bin"查看修改情况
6、读写压缩文件
import gzip with gzip.open("someFile.gz", 'rt') as f: text = f.read() import bz2 with bz2.open('someFile.bz2', 'rt') as f: text = f.read()
7、串行化
import pickle data = ... # some python object f = open("fileName", 'wb') pickle.dump(data, f) #如果存储为字符串使用 pickle.dumps() #Restore from a file f = open("fileName", 'rb') data = pickle.load(f) #Restore from a string data = pickle.loads(f)
迭代式pickle:连续dump/load
>>> import pickle >>> f = open('fileName.pkl', 'wb') >>> pickle.dump([1,2,3,4], f) >>> pickle.dump('hello', f) >>> pickle.dump({'Apple', 'Pear', 'Banana'}, f} >>> f.close >>> f = open('fileName.pkl', 'rb') >>> pickle.load(f) [1, 2, 3, 4] >>> pickle.load(f) 'hello' >>> pickle.load(f) {'Apple', 'Pear', 'Banana'}
db方式:
import shelve #串行化 db = shelve.open('persondb') for object in (bob, sue, tom): db[object.name] = object db.close() #解串行化 db = shelve.open('persondb') len(db) list(db.keys()) bob = db['Bob Smith'] for key in db: print(key, '=>', db[key])
二进制串行化
#串行化 from struct import Struct def write_records(records, format, f): record_struct = Struct(format) for r in records: f.write(record_struct.pack(*r)) if __name__ == '__main__': records = [ (1, 2.3, 4.5), (6, 7.8, 9.0), (12, 13.4, 56.7) ] with open('data.b', 'wb') as f: write_records(records, '<idd', f) #解串行化 from struct import Struct def unpack_records(format, data): record_struct = Struct(format) return (record_struct.unpack_from(data, offset) for offset in range(0, len(data), record_struct.size)) if __name__ == '__main__': with open('data.b', 'rb') as f: data = f.read() for rec in unpack_records('<idd', data): print(rec) #固定记录解串行化 from struct import Struct def read_records(format, f): record_struct = Struct(format) chunks = iter(lambda: f.read(record_struct.size), b'') return (record_struct.unpack(chunk) for chunk in chunks) if __name__ == '__main__': with open('data.b','rb') as f: for rec in read_records('<idd', f): print(rec)
8、lambda
L = [1, 2, 3, 4] list(map(lambda x: x+3), L)) #输出: #[4, 5, 6, 7]
list(filter((lambda x: x > 0), range(-5, 5)))
#输出:
#[1, 2, 3, 4]
list( map((lambda x: x**2), filter((lambda x: x%2) == 0), rang(10))) )
#[0, 4, 16, 36, 64]
9、迭代器、生成器
可迭代的:容器(list, deque, set, frozensets, dict, defaultdict, OrderedDict, Counter, tuple, namedtuple, str)、files、sockets均为可迭代对象
#“可迭代的”指的是支持iter的一个对象,而“迭代器”指的是iter所返回的一个支持next(I)的对象 #“可迭代对象”是序列观念的通用化,如果对象是实际保存的序列,或者可以在迭代工具环境中(例如 for循环)一次产生一个结果的对象,就看做是可迭代的 from functools import partial RECORD_SIZE = 32 with open('data.bin', 'rb') as f: records = iter(partial(f.read, RECORD_SIZE), b'') for r in records: print(r) #偏函数:把一个函数的某些参数固定住(也就是设置默认值),返回一个新函数 #输出: #b' 0 5412 N CLARK' #b' 3 5148 N CLARK'
for循环的执行过程
#for作用在可迭代对象上,首先启用对象的迭代器,然后每次循环自动调用迭代器对象的next()方法产生一个值 #注意:文件对象、生成器函数、生成器表达式都是自身的迭代器 L = [1, 2, 3] for x in L: print x #执行过程 I = iter(L) I.next() #1 I.next() #2 I.next() #3 I.next() #迭代结束产生异常 Traceback(most recent call last): StopIteration #手动迭代变化如下: I = iter(L) while True: try: X = next(I) except StopIteration: break
生成器表达式
>>> mygenerator = (x*x for x in range(3)) #生成器表达式 >>> mygenerator <generator object at 0x011DC648> #生成器迭代对象 #先产生生成器mygenerator,虽然为0/1/4,但不是一次性产生,而是每执行一次for循环产生一个值 >>> for i in mygenerator : ... print(i) #0 1 4 #注意生成器是括号()不是[],生成器每次生成一个值 #for循环作用在可迭代对象上,首先生成可迭代对象的迭代器“iterator”,再调用迭代器的“next”方法获取容器的一个值
生成器函数yield
#Python延迟生成技术,yield每次返回一个结果,在每个结果之间挂起和继续它们的状态,生成器函数自动在生成值的时刻挂起并继续函数的执行 #当调用yiel时,返回一个可迭代对象,该对象支持__next__()方法,迭代完成引发StopIteration异常 def fab(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1 for n in fab(5): print n #输出:1 1 2 3 5 #或者使用迭代器 >> f = fab(5) >> f.next #1 >> f.next #1 >> f.next #2
生成器函数 & 生成器表达式都是自身的迭代器
>>>G = (c*4 for c in 'SPAM') >>>iter(G) is G True
10、类
#属性赋值 x.data = "New value" #增加属性 x.anothername = "spam" #覆盖超类方法,即重载 class SecondClass(FirstClass): #重载构造函数 def __init__(self, name, pay): FistClass.__init__(self, name, 'mgr', pay) def display(self): #运算符重载,使之更像内置类型运算 def __add__(self, other): return SecondClass(self.data + other) def __str__(self): return '[Person: %s, %s]' %(self.name, self.pay) #多态发生在继承类,执行子类还是超类的geveRaise,取决于实例 for object in (bob, sue, tom): object.geveRaise(.10) print (object) #显示object's __ str__
11、JSON
import json data = { 'name' : 'ACME', 'shares' : 100, 'price' : 542.3} #python->json json_str = json.dumps(data) #json->python data = json.loads(json_str) #序列化JSON with open('data.json', 'w') as f: json.dump(data, f)
WEB流
form urlib.request import urlopen import json import pprint u = urlopen('http://kitking01.eicp.net') rep = json.loads(u.read().decode('utf-8')) #pprint打印JSON数据格式 pprint(resp)
12、赋值生成引用,而不是拷贝
L1 = [2, 3, 4] L2 = L1[:]#或者 L2 = copy(L1)
L3 = L1
#传值拷贝、避免参数修改
change(X, L1[:])
13、字串比较
x = 'killer' if x == 'roger': print ("what's up?")
#数字<->字串相互转换
int("42"); str(42)
14、内置类型及函数
#字典操作
D.keys()
D.values()
D.items()
#提取键转换到列表,并排序 D = {} Ks = list(D.keys()) Ks.sort()
#测试键是否存在
if not 'f' in D:
print('missing')
#
x = set(); y = set()
x-y; x|y; x&y; x^y
range(); zip(); map()
15、帮助文档
dir(str)
help(str.replace)
16、调试
pdb、PyChecker、Pylint