9

I have a file which is a list of dictionaries in this way:

[{'text': 'this is the text', 'filed1': 'something1', 'filed2': 'something2', 'subtexts': [{'body': 'body of subtext1', 'date': 'xx'}, {'body': 'body of subtext2', 'date': 'yy'}}]

The list of nested dictionaries and lists can have multiples dicts and lists inside. I want to read the file which is written exactly like this in python and create a data structure (list of dictionaries). How can it be done knowing it's not json but it has written to file by file.write(str(list)) where list is something we want to read back from file?

Nick
  • 367
  • 4
  • 6
  • 13

1 Answers1

17

Use ast.literal_eval which is a safe way of evaluating Python literals (as opposed to eval) into actual data structures.

That is:

import ast

with open('file.txt') as f:
    data = ast.literal_eval(f.read())

If you have something beyond the supported types (strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None), you can use my recipe in this answer.

Community
  • 1
  • 1
  • I am still very much learning; can you explain which way this method is "safe" compared to my answer of just using `eval()`? – roganjosh Apr 13 '16 at 20:19
  • 1
    If the text is from untrusted sources, then with `eval()` it can contain **any** python code, including installing malware on your system. `literal_eval` rejects any python code that is not literals/constants of the types listed in my answer. – Antti Haapala -- Слава Україні Apr 13 '16 at 20:23
  • That still leaves me a bit perplexed. I understand that this is not OP's question, but I can't see how you could, for example, secrete a `print` statement in a data structure that then gets executed by any operation you might perform on a data structure – roganjosh Apr 13 '16 at 20:30
  • Nevermind [found it here](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) thanks :) – roganjosh Apr 13 '16 at 20:47