Home > Enterprise >  opening a python file as an array of dictionaries
opening a python file as an array of dictionaries

Time:12-09

I have a file with the following in it:

[{'page': 0, 'text': 'blah'},{'page': 1, 'text': 'blah'}] 

I'd like to restore this file to an array of dictionaries, but I'm struggling.

I've tried to use js = json.loads(data) to turn it into a dictionary and I've also tried to load it as a string and then split it (but unsure of how to split--based on '[', ']' brackets.

Ideally it would be read as an array of dictionaries.

CodePudding user response:

You can use the eval function to parse lists from text.

data = eval(file.read())

print(data[0]["page"])

# Prints "0".
  • Related