File formats
Please support our Python advertiser: Programming Forums
![]() |
•
•
Posts: 13
Reputation:
Solved Threads: 0
I'm trying to get to grips with using files, particurlarly saving a set of inputs from the user into a file and then load it all back up again.
I have a program which, through a little object oriented interactivity, allows the user to pair up names and numbers in a dictionary. But now I want to be able to save it in file, preferably in form like this:
number;name
I've read a few tutorials and about the cpickle method, but I'm simply lost on how to do something like this in real life. Anyone know of some good - thourough - reading material or even have the time to write an explanation?
I have a program which, through a little object oriented interactivity, allows the user to pair up names and numbers in a dictionary. But now I want to be able to save it in file, preferably in form like this:
number;name
I've read a few tutorials and about the cpickle method, but I'm simply lost on how to do something like this in real life. Anyone know of some good - thourough - reading material or even have the time to write an explanation?
pickling is more beneficial for custom classes. For a built-in class (dictionary, list, etc.) simply use eval (NOTE: this solution has been posted before on this forum, but I couldn't find it)
So for what you're looking for you'd write the repr() bit to a file, and then use eval after reading said file.
*I think the previous solution used execfile instead of bothering to open said file; however like I said I couldn't find it and don't remember exactly, but it answered exactly what you're asking.
Otherwise, use the forum search and look for "pickle" to find examples of dumping/loading pickled objects.
python Syntax (Toggle Plain Text)
>>> d1 = {'a':2, 'b':3} >>> d2 = eval(repr(d1)) >>> d2 {'a': 2, 'b': 3} >>> d2['c']=1 >>> d1 {'a': 2, 'b': 3} >>> d2 {'a': 2, 'c': 1, 'b': 3}
*I think the previous solution used execfile instead of bothering to open said file; however like I said I couldn't find it and don't remember exactly, but it answered exactly what you're asking.
Otherwise, use the forum search and look for "pickle" to find examples of dumping/loading pickled objects.
•
•
Posts: 764
Reputation:
Solved Threads: 142
•
•
•
•
But now I want to be able to save it in file, preferably in form like this:
number;name
def read_from_file():
after_d = {}
fp = open("test.txt", "r")
for rec in fp:
rec=rec.strip()
key, name = rec.split(";")
after_d[int(key)]=name ## convert key back to an integer
fp.close()
return after_d
def save_to_file(save_d):
print "before saving to file", save_d
fp = open("test.txt", "w")
for key in save_d.keys():
fp.write("%d;%s\n" % (key, save_d[key])) ## writes everything as a string
fp.close()
before_d = {}
before_d[1]="Name 1"
before_d[2]="Name 2"
before_d[3]="Name 3"
before_d[10]="Name 10"
before_d[20]="Name 20"
save_to_file(before_d)
after = read_from_file()
print "read from file ", after Last edited by woooee : Nov 19th, 2008 at 12:09 am.
You really should use module pickle for that ...
python Syntax (Toggle Plain Text)
# use module pickle to save/dump and load a dictionary object # or just about any other intact object import pickle # create the test dictionary before_d = {} before_d[1]="Name 1" before_d[2]="Name 2" before_d[3]="Name 3" # pickle dump the dictionary fout = open("dict1.dat", "w") pickle.dump(before_d, fout) fout.close() # pickle load the dictionary fin = open("dict1.dat", "r") after_d = pickle.load(fin) fin.close() print before_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'} print after_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
Last edited by vegaseat : Nov 19th, 2008 at 10:32 am.
May 'the Google' be with you!
![]() |
Similar Threads
Other Threads in the Python Forum
- Storing Data from different file formats to a Database (VB.NET)
- Read encrypted file header (C++)
- *.dat conversion to *.tax file pls help (C++)
- Opening Image file? (C)
- Python and the JPEG Image File, Part 1, The Header (Python)
- Adding file formats on terminal server (Windows NT / 2000 / XP / 2003)
Other Threads in the Python Forum
- Previous Thread: Pay system
- Next Thread: Gathering Wxpy widgets
•
•
•
•
Views: 392 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode