Python:如何保存文件中的对象的列表? [英] Python: how to save a list with objects in a file?
问题描述
我试图创建不同的对象(使用分类和对象),并将其保存在文件中以编辑或检索它们。
im trying to create diferent objects (using Clases and objects) and saving them in a file to edit or retrive them later. However this how it looks.
GlobalCategories=[]
GlobalContent=[]
def LoadData(x,y):
import pickle
with open('bin.dat') as f:
x,y = pickle.load(f)
def SaveData(x,y):
import pickle
with open('bin.dat', 'wb') as f:
pickle.dump([x,y], f)
def Loader(x,y):
try:
LoadData(x,y)
except:
SaveData(x,y)
,这段代码片段保存显示如何保存列表的信息(tema是类和其他东西是该类的方法):
and this the snippet that saves that shows how I save the info the lists (tema is the class and the other stuff are the methods of that class):
newtheme=Tema()
newtheme.setInfo_name(newstr)
newtheme.setInfo_code(newcode)
GlobalCategories.append(newtheme)
SaveData(GlobalContent,GlobalCategories)
X和Y是我存储对象的全局列表(我注意到它保存每个对象的内存中的方向)
当我第一次运行它,它创建文件并将信息保存在文件中,但是如果我关闭它,尝试再次运行它并加载信息,程序擦除信息,并再次创建文件,所以存储的任何东西都消失了。
X and Y are global lists where I store the objects.(i have noticed that it saves the direction in the memory of each object) when i first run it, it creates the file and saves the infomation on the file, however if I close it, try to run it again and load the info, the program erases the information, and creates the file again, so anything that was stored is gone.
我不知道这是否是存储对象的引导方式,或者如果有更好的方式,所以任何建议是非常受欢迎的。
I dont know if this is a propper way to store objects or if there{s a better way so any advice is very welcome.
@abernert:谢谢abarnert!我想做的是保存一个列表里面的两个列表。例如一个列表将保存a make(toyota,nisan等),另一个列表是汽车模型(tundra,murano)。现在每个元素是一个对象,我添加到列表创建时。
newtheme = Theme()
newtheme.setInfo_name(newstr)
GlobalCategories.append(newtheme)
这是我如何将对象保存在全局列表中。全球类别是这两个列表之一,我想加载后,我已经关闭了程序(它将像例子中的汽车公司的名单)。现在,我有问题是从列表中加载对象,因为我已经关闭并重新启动程序,因为我可以从列表中,当我还没有关闭外壳程序,并编辑他们。
我需要加载和存储的make和车对象在相应的列表,一旦我启动程序,所以我可以操纵他们以后。
再次感谢您的问题!
@abernert: Thank you abarnert! what I want to do is to save a list with two lists inside. for example one list is going to save the a make (toyota, nisan etc) and the other list the car model(tundra, murano). now each element is an object wich i add to a list when created.
newtheme=Theme()
newtheme.setInfo_name(newstr)
GlobalCategories.append(newtheme)
this is how i save the object in the global list. GlobalCategories is one of those two list i want to load later after i have closed the program (it would be like the list of car companies from the example). Now, where i have the problem is loading the objects from the lists after i have closed and restarted the program, because i am able to retrive and edit them from the list when i have not closed the shell.
I need to load and store the makes and the cars objects in the respective list once i start the program so i can manipulate them later.
Thank you abernert once again!
推荐答案
很难知道问题是什么,使用您的 LoadData
和 SaveData
函数。但是,这里是一个小演示,我做你想要的。
It's hard to know what the problem is without context of how you are trying to use your LoadData
and SaveData
functions. However, here is a little demo that does what I think you want.
import pickle
import random
def load_data():
try:
with open("bin.dat") as f:
x, y = pickle.load(f)
except:
x, y = [], []
return x, y
def save_data(data):
with open("bin.dat", "wb") as f:
pickle.dump(data, f)
if __name__ == "__main__":
x, y = load_data()
print x, y
x.append(random.randint(1, 10))
y.append(random.randint(1, 10))
save_data([x, y])
从连贯RUNS输出
[] []
[9] [9]
[9, 10] [9, 9]
[9, 10, 2] [9, 9, 4]
[9, 10, 2, 5] [9, 9, 4, 1]
[9, 10, 2, 5, 6] [9, 9, 4, 1, 9]
[9, 10, 2, 5, 6, 10] [9, 9, 4, 1, 9, 1]
这篇关于Python:如何保存文件中的对象的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!