如何使用 pickle python 将包含列表成员的对象列表保存到文件中?
How to save a list of ojects that contains a list as member to file by using pickle python?
我有以下 class:
class Document:
_queryID = ""
_name = []
_docId = ""
在代码中,我创建了一个名为 listDocuments 的列表。我用文档类型的对象填充列表。
import pickle
if __name__ == "__main__":
listDocuments = []
for i in range(0,10):
e = Document()
e._queryID = str(i)
e._docId = str(i+1)
e._name.append("h"+str(i))
listDocuments.append(e)
save_data(listDocuments, "Variations.dat")
list = load_data("Variations.dat")
for obj in listEntities:
print obj._queryID + "==>"+ obj._docId
for var in obj._name:
print var +" "
print "----\n"
我使用了以下来自 Python: how to save a list with objects in a file 的函数:
#save list of variations
def save_data(data, path):
with open(path, "wb") as f:
pickle.dump(data, f)
#load list of variations (Correctd after comments thank you)
def load_data(var_file):
try:
with open(var_file) as f:
listDocument = pickle.load(f)
except:
listDocument = []
return listDocument
- 作为输出,我只能打印 _queryID。我错过了什么?我不能
打印列表 _name 或 _docID
好的,有几个问题。
第一个:
def load_data(var_file):
try:
with open(var_file) as f:
listDocument = pickle.load(f)
except:
listDocuments = []
return listDocuments
您同时使用 listDocument
和 listDocuments
。 (注意一个有尾随 s
)。此外,您在隐藏错误的外部程序中使用了这些变量 listDocuments
。让我们将函数内的变量重命名为 data
,就像 save_data
函数所做的那样。出于同样的一致性原因,我们还将 var_file
重命名为 path
。
考虑:
def load_data(path):
data = []
try:
with open(path) as f:
data = pickle.load(f)
except: pass
return data
接下来,您将使用 list
,这是一个保留的内置函数——让我们将其更改为 lst
。所以现在你有:
class Document:
_queryID = ""
_name = []
_docId = ""
def save_data(data, path):
with open(path, "wb") as f:
pickle.dump(data, f)
#load list of variations
def load_data(path):
data = []
try:
with open(path) as f:
data = pickle.load(f)
except: pass
return data
import pickle
if __name__ == "__main__":
listDocuments = []
for i in range(0,10):
e = Document()
e._queryID = str(i)
e._docId = str(i+1)
e._name.append("h"+str(i))
listDocuments.append(e)
save_data(listDocuments, "Variations.dat")
lst = load_data("Variations.dat")
for elem in lst:
print elem._queryID
print elem._docId
print elem._name
print "---"
有效并打印:
0
1
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
1
2
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
2
3
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
3
4
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
4
5
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
5
6
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
6
7
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
7
8
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
8
9
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
9
10
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
编辑
根据您的编辑,您没有 listEntities
变量 -- 我想您的意思是(最近重命名的)lst
:
for obj in lst:
print obj._queryID + "==>"+ obj._docId
for var in obj._name:
print var +" "
print "----\n"
最后,如果您想知道为什么 _name
变量对于 Document
class 的所有实例都是相同的,那是因为您要声明 _name
作为 class 属性(因此它对所有实例都相同),而不是实例属性。
您可以将 Document
class 定义替换为:
class Document:
def __init__(self):
self._name = []
你可能会对结果更满意:
0==>1
h0
----
1==>2
h1
----
2==>3
h2
----
3==>4
h3
----
4==>5
h4
----
5==>6
h5
----
6==>7
h6
----
7==>8
h7
----
8==>9
h8
----
9==>10
h9
----
我有以下 class:
class Document:
_queryID = ""
_name = []
_docId = ""
在代码中,我创建了一个名为 listDocuments 的列表。我用文档类型的对象填充列表。
import pickle
if __name__ == "__main__":
listDocuments = []
for i in range(0,10):
e = Document()
e._queryID = str(i)
e._docId = str(i+1)
e._name.append("h"+str(i))
listDocuments.append(e)
save_data(listDocuments, "Variations.dat")
list = load_data("Variations.dat")
for obj in listEntities:
print obj._queryID + "==>"+ obj._docId
for var in obj._name:
print var +" "
print "----\n"
我使用了以下来自 Python: how to save a list with objects in a file 的函数:
#save list of variations
def save_data(data, path):
with open(path, "wb") as f:
pickle.dump(data, f)
#load list of variations (Correctd after comments thank you)
def load_data(var_file):
try:
with open(var_file) as f:
listDocument = pickle.load(f)
except:
listDocument = []
return listDocument
- 作为输出,我只能打印 _queryID。我错过了什么?我不能 打印列表 _name 或 _docID
好的,有几个问题。
第一个:
def load_data(var_file):
try:
with open(var_file) as f:
listDocument = pickle.load(f)
except:
listDocuments = []
return listDocuments
您同时使用 listDocument
和 listDocuments
。 (注意一个有尾随 s
)。此外,您在隐藏错误的外部程序中使用了这些变量 listDocuments
。让我们将函数内的变量重命名为 data
,就像 save_data
函数所做的那样。出于同样的一致性原因,我们还将 var_file
重命名为 path
。
考虑:
def load_data(path):
data = []
try:
with open(path) as f:
data = pickle.load(f)
except: pass
return data
接下来,您将使用 list
,这是一个保留的内置函数——让我们将其更改为 lst
。所以现在你有:
class Document:
_queryID = ""
_name = []
_docId = ""
def save_data(data, path):
with open(path, "wb") as f:
pickle.dump(data, f)
#load list of variations
def load_data(path):
data = []
try:
with open(path) as f:
data = pickle.load(f)
except: pass
return data
import pickle
if __name__ == "__main__":
listDocuments = []
for i in range(0,10):
e = Document()
e._queryID = str(i)
e._docId = str(i+1)
e._name.append("h"+str(i))
listDocuments.append(e)
save_data(listDocuments, "Variations.dat")
lst = load_data("Variations.dat")
for elem in lst:
print elem._queryID
print elem._docId
print elem._name
print "---"
有效并打印:
0
1
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
1
2
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
2
3
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
3
4
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
4
5
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
5
6
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
6
7
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
7
8
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
8
9
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
9
10
['h0', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9']
---
编辑
根据您的编辑,您没有 listEntities
变量 -- 我想您的意思是(最近重命名的)lst
:
for obj in lst:
print obj._queryID + "==>"+ obj._docId
for var in obj._name:
print var +" "
print "----\n"
最后,如果您想知道为什么 _name
变量对于 Document
class 的所有实例都是相同的,那是因为您要声明 _name
作为 class 属性(因此它对所有实例都相同),而不是实例属性。
您可以将 Document
class 定义替换为:
class Document:
def __init__(self):
self._name = []
你可能会对结果更满意:
0==>1
h0
----
1==>2
h1
----
2==>3
h2
----
3==>4
h3
----
4==>5
h4
----
5==>6
h5
----
6==>7
h6
----
7==>8
h7
----
8==>9
h8
----
9==>10
h9
----