storing dictionaries into text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muin
    New Member
    • Dec 2012
    • 1

    storing dictionaries into text file

    my dictionary looks like this

    pri={2000:{0.23 3:0,0.444:0}}

    i want to store it in below given format

    2000 0.233 0.444

    how to do that?? thanks in advance
  • roberttonafelix
    New Member
    • Oct 2012
    • 16

    #2
    sorry i am a python developer... i am not getting your question correctly... if you explain that it will be convenient to me to solve...

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Make an assignment to a str object:
      Code:
      >>> pri={2000:{0.233:0,0.444:0}}
      >>> s = "%s %s" % (pri.keys()[0], " ".join(map(str, sorted(pri[pri.keys()[0]]))))
      >>> s
      '2000 0.233 0.444'
      >>>
      In case pri has more than one key:
      Code:
      >>> pri={2000:{0.233:0,0.444:0}, 2001:{0.234:0,0.445:0}}
      >>> s = "\n".join([" ".join(map(str, [subkey]+sorted(pri[subkey].keys()))) for subkey in pri.keys()])
      >>> print s
      2000 0.233 0.444
      2001 0.234 0.445
      >>>

      Comment

      Working...