I'd like to create a dictionary from a text file that I have, who's contents are in a 'dictionary' format. Here's a sample of what the file contains:
{'fawn': [1], 'sermersheim': [3], 'sonji': [2], 'scheuring': [2]}
It's exactly this except it contains 125,000 entries. I am able to read in the text file using read(), but it creates a variable of the literal text of the file even when I initialize the variable with
dict = {}
You can use the
eval
built-in. For example, this would work if each dictionary entry is on a different line:Alternatively, if the file is just one big dictionary (even on multiple lines), you can do this:
This is probably the most simple way to do it, but it's not the safest. As others mentioned in their answers,
eval
has some inherent security risks. The alternative, as mentioned by JBernardo, is to useast.literal_eval
which is much safer than eval since it will only evaluate strings which contain literals. You can simply replace all the calls toeval
in the above examples withast.literal_eval
after importing theast
module.If you're using Python 2.4 you are not going to have the
ast
module, and you're not going to havewith
statements. The code will look more like this:Don't forget to call
inf.close()
. The beauty ofwith
statements is they do it for you, even if the code block in thewith
statement raises an exception.I highly discourage using
eval
though. It may result in security issues if you don't have full control on the input file. Just import your dictionary and save them by using thejson
orpickle
module.Using
eval
might be dangerous. If json doesn't work, then I'd recommend using yaml which seems to work fine with your example input:Use the eval function.
For example,
This looks like json to me. Use thejson
module if so.This looks like yaml to me. Use the pyyaml module if so. (As suggested by @jcollado).
You can't use the
json
module because it is strict about its input.It's not a production ready solution and may not work well with a file of your size, but if you need a simple way and can prepend you file to
then you can rename it to a python file and simply import