RSS Forums RSS

File formats

Please support our Python advertiser: Programming Forums
Reply
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

File formats

  #1  
Nov 18th, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 770
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 139
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: File formats

  #2  
Nov 18th, 2008
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)
  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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote  
Posts: 764
Reputation: woooee has a spectacular aura about woooee has a spectacular aura about woooee has a spectacular aura about 
Solved Threads: 142
woooee woooee is offline Offline
Master Poster

Re: File formats

  #3  
Nov 19th, 2008
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.
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.
Reply With Quote  
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: File formats

  #4  
Nov 19th, 2008
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.
Reply With Quote  
Posts: 2,947
Reputation: vegaseat is a jewel in the rough vegaseat is a jewel in the rough vegaseat is a jewel in the rough 
Solved Threads: 254
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: File formats

  #5  
Nov 19th, 2008
You really should use module pickle for that ...
  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 10:32 am.
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 392 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:10 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC