Feed on
Posts
Comments

to write a python dict to a text file:

f = open(‘text.file’,'w’)

f.write(my_dict)

to read a python dict from a text file:

f = open(‘text.file’,'r’)

my_dict = eval(f.read())


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

11 Responses to “read or write a python dict to/from a text file.”

  1. on 01 Oct 2007 at 2:00 pm Eric Brunson

    Trusted input is a huge factor in using eval(). I’d suggest using the shelve module for persisting data like that, it’s more robust and not subject to code injection attacks for what you’ve done, but is actually more fully featured.

    Details can be found here: http://docs.python.org/lib/node329.html

  2. on 21 Jun 2010 at 2:13 am Jonathan

    I had to change your code to the following to get it to work:

    f = open(’text.file’,’w’)
    f.write(str(my_dict))
    f.close()

    The close function isn’t necessary, but it is good if you are not exiting immediately.

    Thanks for the post.

  3. on 29 Mar 2012 at 11:45 am Mathieu

    def eval1(x):
    try: return eval(x)
    except: return x
    A = { eval1(y[0]) : eval1(y[1]) for y in [x.split() for x in open(filename).readlines()] }

  4. on 30 Aug 2012 at 12:39 pm Graham Jeffries

    a two line solution:

    with open(‘text.file’,'w’) as f:
    f.write(str(my_dict))

  5. on 10 Nov 2012 at 5:19 am Sanjeev Kumar

    proper way of doing

    try:
    output_fav_file=open(self.favFile,”w”)
    output_fav_file.write(str(self.favItems))
    except Exception as er:
    print er
    finally:
    output_fav_file.close()

  6. ポールスミス ネクタイ 人気

    read or write a python dict to/from a text file. » from the desk of stinkpot

  7. on 18 May 2018 at 5:08 pm best place to buy yeezy boost

    I was very happy to find this website. I wanted to thank you for your
    time due to this fantastic read!! I definitely enjoyed every part of it and
    i also have you saved as a favorite to look at new stuff in your web site.

  8. on 27 Jun 2018 at 2:17 am PlumbersUsa

    Hello very nice website! Thank you for this specific info! keep it up.

  9. on 20 Jul 2018 at 2:09 am Erika

    Great web site you have here.. It’s difficult to find quality writing like yours these days.
    I seriously appreciate indivviduals like you!
    Take care!!

  10. on 28 Jul 2018 at 12:54 pm cheap yeezys

    You really make it appear really easy along with your presentation but I find
    this matter to be really one thing that I think I
    might never understand. It kind of feels too complicated and
    extremely wide for me. I’m looking forward to your subsequent put up,
    I will attempt to get the grasp of it!

  11. on 09 Sep 2018 at 12:34 pm gdzie kupic tanie follow na instagramie

    Thanks a lot for sharing this with all people you actually recognise what you’re speaking about!
    Bookmarked. Kindly additionally consult with my website =).
    We could have a hyperlink trade arrangement among us

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.