Home > Net >  read dictionaries from file - python
read dictionaries from file - python

Time:11-03

I'm trying to read a series of dictionaries from a file:

GFG.txt

{'geek': 10, 'geeky': True}
{'GeeksforGeeks': 'Education', 'geekgod': 101.0, 3: 'gfg'}
{'supergeek': 5}

and I found this website with the following solution:

import pickle
geeky_file = open('GFG.txt', 'r')
dictionary_list = pickle.load(geeky_file)
  
for d in dictionary_list:
    print(d)
geeky_file.close()

However this throws an exception:

TypeError: a bytes-like object is required, not 'str'

Opening the file in binary mode:

geeky_file = open('GFG.txt', 'rb')

gives me the error:

UnpicklingError: invalid load key, '{'.

CodePudding user response:

If every "text dictionary" is on a single line, ast.literal_eval can help:

import ast

with open('GFG.txt', 'rb') as f:
    list_of_dicts = [ast.literal_eval(line.decode()) for line in f]
list_of_dicts

Output:

[{'geek': 10, 'geeky': True},
 {'GeeksforGeeks': 'Education', 'geekgod': 101.0, 3: 'gfg'},
 {'supergeek': 5}]

CodePudding user response:

Based this answer, you can load each dict in to a new dict to access them by line number.

import ast

file_dicts = {}
with open('GFG.txt') as f:
    for i, line in enumerate(f):
        file_dicts[i] = ast.literal_eval(line)

Result:

{0: {'geek': 10, 'geeky': True}, 1: {'GeeksforGeeks': 'Education', 'geekgod': 101.0, 3: 'gfg'}, 2: {'supergeek': 5}}

CodePudding user response:

This data is an oddly serialized Python dictionary, not valid JSON. And Pickle is dangerous, and should only be used when you fully understand it.

This will do what you're trying to do, but it won't turn the data into real dicts. If it were valid JSON it would have worked by putting a json.loads into the print function, but JSON uses " double quotes around strings and trying to do it with this data will raise a JSONDecodeError

with open('GFG.txt', 'r') as geeky_file:
    while (line := geeky_file.readline().rstrip()):
        print(line)

The above code opens the file, loops over it line by line so it doesn't load it entirely into memory, trims whitespace, assigns the line variable using the Python 3.8 walrus operator, then prints the line (which is currently a string).

  • Related