943,616 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 644
  • Python RSS
Nov 18th, 2008
0

File formats

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Nov 18th, 2008
0

Re: File formats

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)
python Syntax (Toggle Plain Text)
  1. >>> d1 = {'a':2, 'b':3}
  2. >>> d2 = eval(repr(d1))
  3. >>> d2
  4. {'a': 2, 'b': 3}
  5. >>> d2['c']=1
  6. >>> d1
  7. {'a': 2, 'b': 3}
  8. >>> d2
  9. {'a': 2, 'c': 1, 'b': 3}
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 19th, 2008
0

Re: File formats

Quote ...
But now I want to be able to save it in file, preferably in form like this:

number;name
It's a simple combination so a common text file will work just fine. There is also the advantage of being able to verify what you have written to the file via any text processor. Note that dictionaries hash keys so they are not necessarily in key order.
Python Syntax (Toggle Plain Text)
  1. def read_from_file():
  2. after_d = {}
  3. fp = open("test.txt", "r")
  4. for rec in fp:
  5. rec=rec.strip()
  6. key, name = rec.split(";")
  7. after_d[int(key)]=name ## convert key back to an integer
  8. fp.close()
  9. return after_d
  10.  
  11.  
  12. def save_to_file(save_d):
  13. print "before saving to file", save_d
  14. fp = open("test.txt", "w")
  15. for key in save_d.keys():
  16. fp.write("%d;%s\n" % (key, save_d[key])) ## writes everything as a string
  17. fp.close()
  18.  
  19.  
  20. before_d = {}
  21. before_d[1]="Name 1"
  22. before_d[2]="Name 2"
  23. before_d[3]="Name 3"
  24. before_d[10]="Name 10"
  25. before_d[20]="Name 20"
  26.  
  27. save_to_file(before_d)
  28. after = read_from_file()
  29. print "read from file ", after
Last edited by woooee; Nov 19th, 2008 at 1:09 am.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Nov 19th, 2008
0

Re: File formats

Thanks, this helps a lot. I'm ever so slowly learning to actually like working with python, and every little stupid question answered is a step in that direction.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Nov 19th, 2008
0

Re: File formats

You really should use module pickle for that ...
python Syntax (Toggle Plain Text)
  1. # use module pickle to save/dump and load a dictionary object
  2. # or just about any other intact object
  3.  
  4. import pickle
  5.  
  6. # create the test dictionary
  7. before_d = {}
  8. before_d[1]="Name 1"
  9. before_d[2]="Name 2"
  10. before_d[3]="Name 3"
  11.  
  12. # pickle dump the dictionary
  13. fout = open("dict1.dat", "w")
  14. pickle.dump(before_d, fout)
  15. fout.close()
  16.  
  17. # pickle load the dictionary
  18. fin = open("dict1.dat", "r")
  19. after_d = pickle.load(fin)
  20. fin.close()
  21.  
  22. print before_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
  23. print after_d # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
Last edited by vegaseat; Nov 19th, 2008 at 11:32 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Approximation of Pi (Python)
Next Thread in Python Forum Timeline: Gathering Wxpy widgets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC