cPickle 类将数据保存到文件
我在 Python 中有一个大类,它是“类似数据库”的类。我想将其保存到文件 - 所有包括数据。
这是输入(显示问题的示例,在脚本数据库中大约有 10000 条记录):
import cPickle
# DataBase-like class
class DataBase:
class Arrays: pass
class Zones: pass
class Nodes:
class CR: pass
class Links:
class CR: pass
class Turns:
class CR: pass
class OrigConnectors: pass
class DestConnectors: pass
class Paths: pass
pass
# some basic input into database
DataBase.Arrays.Data=[]
for i in range(1000):
DataBase.Arrays.Data.append([i+4])
print DataBase.Arrays.Data[56]
# and now I want to save it to file
import cPickle
filename='D:/results/file.lft'
file=open(filename,'w')
cPickle.dump(DataBase, file, protocol=2)`
这就是我得到的文件:
€c__main__
DataBase
q.`
没有数据,只有定义。我怎样才能克服这个问题。也许将我的数据保存为一个类不是一个好主意?
I've big class in Python it's "DataBase-like" class. I want to save it to file - all including data.
This is input(example to show the issue, in script database is like 10000 records):
import cPickle
# DataBase-like class
class DataBase:
class Arrays: pass
class Zones: pass
class Nodes:
class CR: pass
class Links:
class CR: pass
class Turns:
class CR: pass
class OrigConnectors: pass
class DestConnectors: pass
class Paths: pass
pass
# some basic input into database
DataBase.Arrays.Data=[]
for i in range(1000):
DataBase.Arrays.Data.append([i+4])
print DataBase.Arrays.Data[56]
# and now I want to save it to file
import cPickle
filename='D:/results/file.lft'
file=open(filename,'w')
cPickle.dump(DataBase, file, protocol=2)`
And that's the file I get:
€c__main__
DataBase
q.`
There's no data, just definition. How can I overcome that. Maybe saving my data as a class is not a good idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在腌制“DataBase”,这是一个类定义。您需要实例化 DataBase 类的对象,然后对其进行 pickle。
You are pickling "DataBase" which is a class definition. You need to instantiate an object of class DataBase then pickle that.