0

could you please advice how to read data from file as a dict.

file contains the following lines:

{'foo1': (0, 10), 'foo2': (0, 9), 'foo3': (0, 20)}
{'foo4': (0, 16), 'foo5': (0, 7), 'foo6': (0, 13), 'foo7': (0, 11)}
{'foo8': (0, 8), 'foo9': (0, 8), 'foo10': (0, 7)}
{'foo11': (0, 8)}

all data in {'key': (value, value)} format. All keys in the file are different.

I'd like to get the following "dict":

{'foo1': (0, 1), 'foo2': (0, 0), 'foo3': (0, 1), 'foo4': (1, 0), 'foo5': (0, 0), 'foo6': (0, 5), 'foo7': (0, 2), 'foo8': (2, 2), 'foo9': (1, 1), 'foo10': (0, 7), 'foo11': (0, 1)}

is it possible to extract dicts from the file as merged dict?

For a moment I get only "list" from the file and stucked at this step

import ast
with open('filename') as f:
    content = [ ast.literal_eval( l ) for l in f.readlines() ] 
    print(content)
    

Output:

[{'foo1': (0, 10), 'foo2': (0, 9), 'foo3': (0, 20)}, {'foo4': (0, 16), 'foo5': (0, 7), 'foo6': (0, 13), 'foo7': (0, 11)}, {'foo8': (0, 8), 'foo9': (0, 8), 'foo10': (0, 7)}, {'foo11': (0, 8)}]
  • What have you tried so far? – Sumner Evans Jan 07 '21 at 01:24
  • 1
    Does this answer your question? [Loading and parsing a JSON file with multiple JSON objects](https://stackoverflow.com/questions/12451431/loading-and-parsing-a-json-file-with-multiple-json-objects) – Jab Jan 07 '21 at 01:36

3 Answers3

0

Check out ast.literal_eval(): https://www.kite.com/python/answers/how-to-read-a-dictionary-from-a-file-in--python

Paul Fornia
  • 452
  • 3
  • 9
  • sorry, just saw your edit. So this question is really more about just merging dictionaries: https://stackoverflow.com/questions/2365921/merging-python-dictionaries – Paul Fornia Jan 07 '21 at 01:32
  • thanks, but the problem is how to extract each dictionary from file as a dictionary before to be able to merge them – Andrey Andrey Jan 07 '21 at 01:45
  • yes, but it looks like you already have that step done, no? Your output looks like a list of already extracted dictionaries. – Paul Fornia Jan 07 '21 at 01:58
  • yes each string was the dictionary before it came into the file. but now I stucked of how to extract them back from the file. I used ast if it was only one dictionary it works fine. but it crushes if my file contains more than one dict. – Andrey Andrey Jan 07 '21 at 02:14
0

If you are totally confident in this file being innocent and only a dictionary, then you can use the python built-in function eval to get the job done. e.g.:

myfile = open("file.txt", "r")
mydict = eval(myfile.read())

If this allows any user input into the file, however, this could be used to potentially run arbitrary code on your machine. There are precautions to be took if this is reliant on user input, see the top answer on Python: make eval safe for some ideas.

Theo
  • 613
  • 4
  • 22
  • thanks but looks like it does not work for multiple dicts. I got an error at 2nd line of my file. {'foo4': (0, 16), 'foo5': (0, 7), 'foo6': (0, 13), 'foo7': (0, 11)} ^ SyntaxError: invalid syntax – Andrey Andrey Jan 07 '21 at 01:36
  • @AndreyAndrey looks like the solution you have in your question now should work - it gave you a list of dictionaries. In python 3.9+, you can use the `|` operator to merge dictionaries. Before that, however, you can use `nameofdict.update(otherdict)`, so a code snippet would be: ``` final = {} for d in content: final.update(d) ``` – Theo Jan 08 '21 at 00:22
0

As you get a list of dictionary in content due to list comprehension. I will modify a little.

import ast
content = {}
with open('filename') as f:
    content = content.update(content,ast.literal_eval( l ) for l in f.readlines())
print(content)

See if it works I am beginner. I learned how to merge dictionary from below link. https://levelup.gitconnected.com/7-different-ways-to-merge-dictionaries-in-python-30148bf27add

  • thanks but I get this result: ValueError: dictionary update sequence element #0 has length 3; 2 is required – Andrey Andrey Jan 07 '21 at 01:40
  • Sorry, It's slightly wrong, content.update(ast.literal_eval( l ) for l in f.readlines()),it is content.update(content,ast.literal_eval( l ) for l in f.readlines()) – Rabi Raj Yadav Jan 07 '21 at 04:35