Python Pickle 不存储数据
我有一个包含 { Name : Email Address } 的字典
我有一个单独的 .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()
它有效,因为它将原始字典腌制到 orig.pkl
然后在我的主网站中,我有:
# 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()
然后我有两个表单输入在采用电子邮件地址和相应名称的网站上
#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
但是没有新数据存储到 tmp.pkl 或 orig.pkl
有什么想法可以让我开始吗?
谢谢
I have a dictionary containing { Name : Email Address }
I have a seperate .py to pickle this:
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
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
Any ideas to get me started?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 pickle 作为网站的动态更新数据存储并不是很好。为了避免并发问题,您必须实现锁定文件机制,并希望访问该文件的其他所有内容都会尊重它。
我强烈建议您使用支持并发访问的数据存储。例如数据库。
阅读:http://en.wikipedia.org/wiki/Concurrency_control
您可以开始使用 sqlite 很容易。看:
http://docs.python.org/library/sqlite3.html
Using pickle as a dynamically updated data store for a website isn't great. 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. E.g. a database.
Have a read of: http://en.wikipedia.org/wiki/Concurrency_control
You could start easy with sqlite. See:
http://docs.python.org/library/sqlite3.html
MattH 是对的,你绝对不应该使用 pickle 作为数据库替代品。我建议使用类似 mongo 的东西,因为它使存储字典变得轻而易举。我发现 pymongo 真的很容易使用,并且吞咽字典没有问题。
MattH is right, you definitely should NOT use pickle as a database replacement. I suggest using something like mongo since it makes storing dictionaries as breeze. I've found pymongo is real easy to use and slurps up dictionaries no problem.
python shelve 模块负责为您提供类似字典的对象,但也会在您要求时将对象腌制并存储到文件中。正如其他人所说,如果要经常更新,您需要使用某种数据库,但在易用性方面很难击败搁置模块。
http://docs.python.org/library/shelve.html
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. 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