如何使用pickle python将包含列表作为成员的对象列表保存到文件中?

时间:2015-03-06 08:38:14

标签: python pickle

我有以下课程:

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

1 个答案:

答案 0 :(得分:2)

好的,有几个问题。

首先:

def load_data(var_file):
    try:
        with open(var_file) as f:
            listDocument = pickle.load(f)
    except:
        listDocuments = []

    return listDocuments

您同时使用listDocumentlistDocuments。 (注意一个有一个尾随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变量为何相同,那是因为您将_name声明为类属性(因此它对所有实例都是一样的),而不是实例属性。

您可以将Document类定义替换为:

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  
----