Skip to content Skip to sidebar Skip to footer

Import Dict From Plain Text File

Let's say I have a dict in text file but I've lost the python code which created it. Is it still possible to query info from python to this dict? What about creating a python scri

Solution 1:

with open('dictionaryFile.txt','r') as filein:
    dict_from_file = eval(filein.read())

Solution 2:

Assuming you have some file dict.txt that has a plaintext dictionary and nothing else, you can use pprint, the pretty-printing module

from pprint import pprint
from ast import literal_eval

with open('dict.txt') as f:
    pprint(literal_eval(f.read())

Post a Comment for "Import Dict From Plain Text File"