浮点数 非宁静,无以致远(6)
拆分编组同样不难:
1. import pickle
2.
3. fileHandle = open ( 'pickleFile.txt' )
4. testList = pickle.load ( fileHandle )
5. fileHandle.close()
import pickle
fileHandle = open ( 'pickleFile.txt' )
testList = pickle.load ( fileHandle )
fileHandle.close()
现在试试存储更加复杂的数据:
1. import pickle
2.
3. fileHandle = open ( 'pickleFile.txt', 'w' )
4. testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', [ 1, 2, 7 ] ]
5. pickle.dump ( testList, fileHandle )
6. fileHandle.close()
import pickle
fileHandle = open ( 'pickleFile.txt', 'w' )
testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', [ 1, 2, 7 ] ]
pickle.dump ( testList, fileHandle )
fileHandle.close()
1. import pickle
2.
3. fileHandle = open ( 'pickleFile.txt' )
4. testList = pickle.load ( fileHandle )
5. fileHandle.close()
import pickle
fileHandle = open ( 'pickleFile.txt' )
testList = pickle.load ( fileHandle )
fileHandle.close()
如上所述,使用Python的“pickle”模块编组确实很简单。众多对象可以通过它来存储到文件中。如果可以的话,“cPickle”同样胜任这个工作。它和“pickle”模块一样,但是速度更快:
1. import cPickle
2.
3. fileHandle = open ( 'pickleFile.txt', 'w' )
4. cPickle.dump ( 1776, fileHandle )
5. fileHandle.close()
import cPickle
fileHandle = open ( 'pickleFile.txt', 'w' )
cPickle.dump ( 1776, fileHandle )
fileHandle.close()
字符串匹配
对于简单的数据,使用流文本文件而不是数据库更简单明了,也就少不了文件操作和字符串匹配的需求。
re模块的search和match方法是匹配到就返回,而不是去匹配所有,而findall()则匹配所有返回数组。
>>> m=re.findall("^a\w+","abcdfa\na1b2c3",re.MULTILINE)
>>> m
['abcdfa', 'a1b2c3']
很好的电影