0

I have a txt file - chunk6.txt - with many thousand dicts but in a str format. I have tried to convert to json, but can't make it work. I want to extract every dict, and the first key in every dict is "cap" as you can see from the output below. Does somebody in here have any ideas how to do this?

with open("chunk6.txt") as f: 
    data = f.read()

pprint(data)

('{\n'
 '  "cap": {\n'
 '    "english": 0.0016347149115980926,\n'
 '    "universal": 0.0020139366550120737\n'
 '  },\n'
 '  "categories": {\n'
 '    "content": 0.07065345957790067,\n'
 '    "friend": 0.10612616098731911,\n'
 '    "network": 0.08835149684131184,\n'
 '    "sentiment": 0.09212893023754924,\n'
 '    "temporal": 0.05938086027486572,\n'
 '    "user": 0.02998627881882177\n'
 '  },\n'
 '  "display_scores": {\n'
 '    "content": 0.4,\n'
 '    "english": 0.2,\n'
 '    "friend": 0.5,\n'
 '    "network": 0.4,\n'
 '    "sentiment": 0.5,\n'
 '    "temporal": 0.3,\n'
 '    "universal": 0.2,\n'
 '    "user": 0.1\n'
 '  },\n'
 '  "scores": {\n'
 '    "english": 0.04178983547133819,\n'
 '    "universal": 0.037980136466376074\n'
 '  },\n'
 '  "user": {\n'
 '    "id_str": "410688798",\n'
 '    "screen_name": "anonymized"\n'
 '  }\n'
 '}{\n'
 '  "cap": {\n'
 '    "english": 0.008750694009244317,\n'
 '    "universal": 0.007183846426565749\n'
Shelf
  • 11
  • 1
  • You use a json parser. The part you posted is invalid json, so that wont work,but you cut it from your original data so it might be possible using your whole file. – Patrick Artner Apr 12 '20 at 10:30
  • What means "can't make it work"? Show what you tried (as properly formatted text in the question). – Michael Butscher Apr 12 '20 at 10:31

0 Answers0