2

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.

Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 2
    Does this answer your question? [How to convert string representation of list to a list?](https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list) – Iain Shelvington Dec 09 '21 at 01:03

1 Answers1

1

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

data = eval(file.read())

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

# Prints "0".
kggn
  • 73
  • 1
  • 8