简体   繁体   English

Python Pickle不存储数据

[英]Python Pickle Not Storing Data

I have a dictionary containing { Name : Email Address } 我有一个包含{姓名:电子邮件地址}的字典

I have a seperate .py to pickle this: 我有一个单独的.py来腌制这个:

emailDict = {'Kilizo': 'info%40kilizo.com' , 'about': 'about%40google.com' }


# write python dict to a file

output = open('orig.pkl', 'wb')
pickle.dump(emailDict, output)
output.close()

which works, in that it pickles the original dictionary to orig.pkl 这是有效的,因为它将原始字典腌制为orig.pkl

Then in my main website, i have: 然后在我的主网站上,我有:

# Pickling # Deleting Old Temp & Creating New One
tmp = os.path.isfile("tmp.pkl")
if tmp == True:
        os.remove("tmp.pkl")
shutil.copyfile("orig.pkl", "tmp.pkl")

# Pickling # Loading File
pkl_file = open('tmp.pkl', 'rb')
emailDict = pickle.load(pkl_file)
pkl_file.close()

I then have two form inputs on a website that take the email address and corresponding name 然后,我在网站上有两个表单输入,其中包含电子邮件地址和相应的名称

#Processing input
emailAdded = fs.getvalue('emailAdd')
nameAdded = fs.getvalue('nameAdd')
if  emailAdded != None or nameAdded != None:
    print emailAdded
    print nameAdded 
    emailDict[nameAdded] = emailAdded
else:
    print "Please enter a name & email address" 
output = open('tmp.pkl', 'wb')
pickle.dump(emailDict, output)
output.close()
print emailDict

However no new data gets stored to either tmp.pkl or orig.pkl 但是,没有新数据存储到tmp.pkl或orig.pkl

Any ideas to get me started? 有什么想法让我开始?

Thanks 谢谢

Any ideas to get me started? 有什么想法让我开始?

Using pickle as a dynamically updated data store for a website isn't great. 使用pickle作为网站的动态更新数据存储并不是很好。 In order to avoid concurrency issues you'll have to implement a lockfile mechanism and hope that everything else that accesses the file will respect it. 为了避免并发问题,您必须实现一个锁定文件机制,并希望访问该文件的所有其他内容都会尊重它。

I strongly suggest that you use a data store that supports concurrent access. 我强烈建议您使用支持并发访问的数据存储。 Eg a database. 例如数据库。

Have a read of: http://en.wikipedia.org/wiki/Concurrency_control 阅读: http//en.wikipedia.org/wiki/Concurrency_control


You could start easy with sqlite. 你可以用sqlite轻松开始。 See: http://docs.python.org/library/sqlite3.html 请参阅: http//docs.python.org/library/sqlite3.html

MattH is right, you definitely should NOT use pickle as a database replacement. MattH是对的,你绝对不应该使用pickle作为数据库的替代品。 I suggest using something like mongo since it makes storing dictionaries as breeze. 我建议使用类似mongo的东西,因为它使存储字典变得轻而易举。 I've found pymongo is real easy to use and slurps up dictionaries no problem. 我发现pymongo真的很容易使用,并且没有问题。

The python shelve module takes care of giving you a dictionary-like object, but also pickles and stores objects to a file when you ask it to. python shelve模块负责为您提供类似字典的对象,但是当您提出要求时,它还会将对象存储并存储到文件中。 As others have said, if it is going to be updated very often you want to use some sort of database but it is hard to beat the shelve module for ease of use. 正如其他人所说,如果要经常更新,你想使用某种数据库,但为了易于使用,很难击败搁置模块。

http://docs.python.org/library/shelve.html http://docs.python.org/library/shelve.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM