你能帮忙做这个例子吗?
我想加载序列化的dict(如果存在),修改它并再次转储它.我认为我使用打开文件的模式有问题,但我不知道正确的方法.
import os
import cPickle as pickle
if os.path.isfile('file.txt'):
cache_file = open('file.txt', 'rwb')
cache = pickle.load(cache_file)
else:
cache_file = open('file.txt', 'wb')
cache = dict.fromkeys([1,2,3])
# modifications of cache
pickle.dump(cache, cache_file)
cache_file.close()
Run Code Online (Sandbox Code Playgroud)
运行两次以查看错误:
Traceback (most recent call last):
File "example.py", line 11, in <module>
pickle.dump(cache, cache_file)
IOError: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud) 我一直在python 2.6中使用json模块,但速度很慢.我想使用更快的实现.我见过cjson,但似乎开发并没有进行,并且api与json模块不同.我也读过一些关于加速json的方法的评论.
有任何想法吗?