0

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?

Atirag
  • 1,660
  • 7
  • 32
  • 60
  • You have JSON objects, use the `json` module to parse those. `null` is not a Python literal, for example, it is a JSON encoding for what Python calls `None`. – Martijn Pieters Sep 06 '15 at 12:51
  • See the solution in the duplicate post I linked you to on how to load these into Python objects. – Martijn Pieters Sep 06 '15 at 12:52
  • try using pickle module: https://docs.python.org/2/library/pickle.html pickle can let you saving objects into files and load them later with pickle.dump and pickle.load – equinox93 Sep 06 '15 at 14:08
  • I tried what you suggested but something is not working on the jparse function. I'm getting a ValueError exception when then line result, index = decoder.raw_decode(buffer) comes along. I'm not exactly sure what this line is trying to do though. – Atirag Sep 07 '15 at 11:43
  • So I guess the problem is that the decoder is not identifying the document as a JSON one. Each entry on the document is right after another so for example as one ends with } another inmediately begins with {. Would that make any difference? – Atirag Sep 07 '15 at 11:48

0 Answers0