Home:ALL Converter>How to write dictionary to text, then read the text to dictionary in python

How to write dictionary to text, then read the text to dictionary in python

Ask Time:2015-07-13T09:41:04         Author:Mark

Json Formatter

My goal is to write a dictionary to text (so that I don't have to keep accessing a database), then save the information in the text file as a dictionary. This is what I tried:

To write the dictionary to text, I used

 with open("match_histories.txt", "w") as text_file:
    print("match_histories: {}".format(match_histories), file=text_file)

This seemed to work nicely and my text file looks like:

match_histories: {'28718115': {'matches': [{'matchVersion': '5.13.0.329', ...

I want to save this as a dictionary, so I tried:

match_histories = {}
f=open('match_histories.txt', 'r')
match_histories= eval(f.read())

However, when I run it, I get an error in trying to save the new dictionary. I get the following error

Traceback (most recent call last):

File "C:\Python34\Main.py", line 87, in

main()

File "C:\Python34\Main.py", line 82, in main

new_dict = eval(f.read())

File "", line 1

How should I save the information from my text file as a dictionary in Python?

Edit: Thanks to namooth, the problem was that my text file was not in a valid dictionary format. How can I not write the name of my dictionary to the file?

Edit 2: Wow, everyone is super helpful! I think I've got it now.

Edit 3: I tried the pickle dump that was suggested, but i got this error:

Traceback (most recent call last):

File "C:\Python34\Main.py", line 88, in

main()

File "C:\Python34\Main.py", line 79, in main

match_histories=get_match_histories(challenger_Ids)

File "C:\Python34\Main.py", line 47, in get_match_histories

pickle.dump(match_histories, "match_histories.txt")

TypeError: file must have a 'write' attribute

write:

pickle.dump(match_histories, "match_histories.txt")

read:

match_histories = pickle.load("match_histories.txt")

Do I still need a line opening the file? How do I fix this error?

Author:Mark,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31374353/how-to-write-dictionary-to-text-then-read-the-text-to-dictionary-in-python
bobrobbob :

The json module is perfect for me as pickle produces barely readable content",
2015-07-13T01:51:12
Blckknght :

The issue with your current code is that you're adding a prefix \"match_histories: \" before the repr of your dictionary. Python can't parse that part of the text, so you get an error when you eval it.\n\nTry just writing the repr of the dictionary by itself:\n\nwith open(\"match_histories.txt\", \"w\") as text_file:\n print(repr(history), file=text_file)\n\n\nThis will work if all of the objects contained at any level in the dictionary have reprs that can be parsed back in properly. It won't work if the dictionary contains objects with unhelpful reprs, or if it contains recursive references.\n\nA better approach is probably to use the pickle modules to save an load your data to a file:\n\npickle.dump(history, \"match_histories.txt\")\n\n\nAnd later:\n\nnew_dict = pickle.load(\"match_histories.txt\")\n\n\nYou could also use the json module if you want the text file to be human readable.",
2015-07-13T01:54:56
yy