开发者

Python Pickle Not Storing Data

开发者 https://www.devze.com 2023-04-03 14:35 出处:网络
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\' }

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.copyfi开发者_Go百科le("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


Any ideas to get me started?

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号