1

I was trying a small example where I was trying to dump a data structure to a file , just like I would do in Perl with the help of Data::Dumper. The following is the example I tried

import pickle
ff = open('/Users/arunpotharaju/hell','wb')
favorite_color = { "lion": "yellow", "kitty": "red" }
pickle.dump(favorite_color,ff,0)

The following is the output in the file hell

(dp0
S'lion'
p1
S'yellow'
p2
sS'kitty'
p3
S'red'
p4
s.

I was looking for more of a Data::Dumper style output from Perl, which is very helpful in debugging. Can I make it any better?

will-hart
  • 3,742
  • 2
  • 38
  • 48

1 Answers1

2

In that case, you could use json to dump a readable version of your dict:

import json
ff = open('/Users/arunpotharaju/hell','wb')
favorite_color = { "lion": "yellow", "kitty": "red" }
ff.write(json.dumps(favorite_color))

json is not able to dump complex data structures the way pickle works, but it can dump dicts, lists, etc.

Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97