Thread: File formats
View Single Post
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: File formats

 
0
  #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