不从pickle文件加载

时间:2017-04-08 15:15:48

标签: python pickle

我在macOS Sierra上运行Python 3.5。我有一个系统,我将一个类的实例作为列表存储在一个文件中(使用dill),然后取消它们以执行操作,添加它们,删除它们等等。问题是当我设置localData列表(加载的类列表)到从文件加载的数据,我无法与它进行交互。


这是我的代码:

import dill as pickle


...


dataFileLocation = "./data.ehd"
localData = []


class eventClass:
    def __init__(self, name, items, organisers):
        self.name       =   name
        self.items      =   items
        self.organisers =   organisers
        loadData()
        self.id         =   len(localData)

def loadData():
    try:
        dataFile            =   open(dataFileLocation, "rb")
        localData           =   pickle.load(dataFile)
        return localData
    except (IOError, EOFError):
        return False


def newEvent(name, items, organisers):
    loadData()
    localData.append(eventClass(name, items, organisers))
    with open(dataFileLocation, "wb+") as dataFile:
        pickle.dump(localData, dataFile)


...


def getInfo(name):
    loadData()
    for event in localData:
        if event.name == name:
            organisers      =   ""
            for organiser in event.organisers:
                organisers  =   (organisers + ", " + str(organiser))
            items = ""
            for item in event.items:
                items       =   (items + ", " + str(item))
            items           =   items[2:]
            organisers      =   organisers[2:]
            print("Name:        " + str(event.name))
            print("Internal ID: " + str(event.id))
            print("Items:       " + items)
            print("Organisers:  " + organisers)


def prompt():
    task = input("EventHorizon >")
    execute(task)
    prompt()


def execute(task):
    task                    =   task.lower().split(" ")
    if task[0] == ("newevent"):
        name                =   task[1]
        items               =   task[2].split(":")
        organisers          =   task[3].split(":")
        newEvent(name, items, organisers)
    elif task[0] == ("dropevent"):
        name                =   task[1]
        dropEvent(name)
    elif task[0] == ("getinfo"):
        name                =   task[1]
        getInfo(name)
    elif task[0] == ("quit") or task[0] == ("exit"):
        quit()
    elif task[0] == ("loaddata"):
        loadData()




loadData()
prompt()


这就是我正在做的事情(从空白data.ehd开始):

bash-3.2$ python3 main.py
EventHorizon >newEvent myEvent myItem myOrganiser
EventHorizon >newEvent myEvent2 myItem2 myOrganiser2
EventHorizon >getInfo myEvent
Name:           myevent
Internal ID:    0
Items:          myitem
Organisers:     myorganiser
EventHorizon >getInfo myEvent2
Name:           myevent2
Internal ID:    1
Items:          myitem2
Organisers:     myorganiser2
EventHorizon >exit
bash-3.2$ python3 main.py
EventHorizon >loadData
EventHorizon >getInfo myEvent
EventHorizon >getInfo myEvent2

没有显示事件属性的输出。我还从确实包含数据的data.ehd文件开始对此进行了测试(写入工作正常)。

为什么会这样? 任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

感谢@ juanpa.arrivillaga,这个问题已经解决了!

问题是echo "0";内的localData变量只是本地变量,因此主代码中的loadData未更新。

通过在开始时修改localData以将loadData定义为全局来解决此问题。修改后的localData是:

loadData