我在 python 上加载数据时遇到两个问题,这两个 scipts 都可以正常工作,但它们需要太多时间才能运行,有时结果是“Killed”(第一个)。
我有一个很大的压缩文本文件,我会这样做:
import gzip import cPickle as pickle f = gzip.open('filename.gz','r') tab={} for line in f: #fill tab with open("data_dict.pkl","wb") as g: pickle.dump(tab,g) f.close()我必须对我在之前的脚本中创建的字典进行一些操作
import cPickle as pickle with open("data_dict.pkl", "rb") as f: tab = pickle.load(f) f.close() #operations on tab (the dictionary)
您还有其他解决方案吗?也许不是那些涉及 YAML 或 JSON 的......
请您参考如下方法:
如果你正在 pickle 的数据是原始和简单的,你可以试试 marshal 模块:http://docs.python.org/3/library/marshal.html#module-marshal .这就是 Python 用来序列化其字节码的方式,因此速度非常快。
