Feed on

to write a python dict to a text file:

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


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!

12 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’)

    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:

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

    proper way of doing

    except Exception as er:
    print er

  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

  12. on 28 Nov 2018 at 8:03 am Bandar Domino

    This is absolutgely the easiest method to train yourself
    if you need to be a serious player. She is playing the piano by age four and used that skill
    like a teenager to try out through New York’s open mic
    nights at age 14. Whenn you are seeking the righ poker felt
    top, there are numerous opotions available, so take a momennt to ensure that you
    have the right choices open to you.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

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