Python入门的36个例子 之 28
源代码下载:下载地址在这里
# 032
# 其实cPickle这个模块起到的作用可以用“完美地协调了文件中的内容(对象)和代码中的引用”来形容
import cPickle as p # 这条语句给cPickle起了个小名p
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close()
del aList # 现在已经没有aList所指的对象了,甚至没有aList这个引用了
f = file(objectFileName, 'r')
storedObject = p.load(f)
print storedObject
output:
>>>
[1, 2, 3]
>>>
当然,在文件中,Python采用了一种便于自己分辨对象类型的格式来存储:
相关文档:
从MoteLab返回的串口数据,包含messages.pickle文件这是MoteLab系统中串口收集数据的总和,但是这些数据需要解析后才能进行分析。下面的代码就是在python环境下提取message有效数据的代码。
使用命令 python TestOutput.py messages.pickle
生成的test.log就是获得的有效数据
返回数据的示例
1252985727.66 recei ......
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......
代码很简单,不到5k行。但是思路挺好的,改成non-blocking了之后效率就是能提高不少,特别是考虑到现代的web app都需要和其他的
HTTP服务器通信,blocking的代价太大了。 Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed. The FriendFeed application ......
源代码下载:下载地址在这里
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# 也可以使用整型作为唯一的编号
dict2 = {
5064001:'Mememe',
506400 ......