I have a text file with many entries like this one after the other
{
"class": 0,
"geo": null,
"id": 634485054933200897,
....
}
I want to turn each of these entries into a dictionary and put them on a list of dictionaries. This is what I've done so far.
data=""
d={}
flag=False
with open('ctp.txt','r') as f:
for line in f:
if line.startswith('{'):
flag=True
if flag:
data+=line
if line.strip().endswith('}'):
flag=False
d=ast.literal_eval(data)
data=""
So the text is appended correctly to the string variable called data but the call to ast.literal_eval(data) fails with this error
File "...\lib\ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "...\lib\ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 0
^
SyntaxError: unexpected EOF while parsing
Any ideas why the string isn't correctly formatted?