Reputation: 43
I am new at the coding. I am trying to read json file and reach some values then put them into list.
I tried;
with open('score.json', 'r') as f:
score = json.load(f)
def match():
r_=score["r"]
for r in r_:
t_a=r["types"]
for types_d in t_a:
v_a=t_d["v"]
for v_d in v_a:
key.append(v_d)
key=[]
TypeError: list indices must be integers or slices, not str
I got this error. Could you help me? Thanks
Upvotes: 0
Views: 61
Reputation: 589
The JSON you put here is a list not a dict. when you load this JSON to kscore make that a list in the first line of match function you tried to get "r" attribute of that and error says you can't do that. to solve the problem make the JSON like a dict like below:
{"numberOfHits": 2, "results": [{"groupName": "annotation", "totalHits": 12, "types": [{"type": "reference", "approximateCount": 9, "values": [{"key": "GO_REF:0000052", "percentage": 16.666666666666664, "hits": 2, "name": null}, {"key": "PMID:19203586", "percentage": 16.666666666666664, "hits": 2, "name": null}, {"key": "PMID:20333297", "percentage": 16.666666666666664, "hits": 2, "name": null}, {"key": "PMID:10983986", "percentage": 8.333333333333332, "hits": 1, "name": null}, {"key": "PMID:14749367", "percentage": 8.333333333333332, "hits": 1, "name": null}, {"key": "PMID:16792529", "percentage": 8.333333333333332, "hits": 1, "name": null}, {"key": "PMID:20878056", "percentage": 8.333333333333332, "hits": 1, "name": null}, {"key": "PMID:21045808", "percentage": 8.333333333333332, "hits": 1, "name": null}, {"key": "PMID:25190803", "percentage": 8.333333333333332, "hits": 1, "name": null}]}, {"type": "assignedBy", "approximateCount": 3, "values": [{"key": "UniProt", "percentage": 75.0, "hits": 9, "name": null}, {"key": "HPA", "percentage": 16.666666666666664, "hits": 2, "name": null}, {"key": "MGI", "percentage": 8.333333333333332, "hits": 1, "name": null}]}, {"type": "goId", "approximateCount": 8, "values": [{"key": "GO:0005886", "percentage": 25.0, "hits": 3, "name": "plasma membrane"}, {"key": "GO:0005737", "percentage": 16.666666666666664, "hits": 2, "name": "cytoplasm"}, {"key": "GO:0032991", "percentage": 16.666666666666664, "hits": 2, "name": "protein-containing complex"}, {"key": "GO:0005634", "percentage": 8.333333333333332, "hits": 1, "name": "nucleus"}, {"key": "GO:0005654", "percentage": 8.333333333333332, "hits": 1, "name": "nucleoplasm"}, {"key": "GO:0005829", "percentage": 8.333333333333332, "hits": 1, "name": "cytosol"}, {"key": "GO:0015630", "percentage": 8.333333333333332, "hits": 1, "name": "microtubule cytoskeleton"}, {"key": "GO:0031982", "percentage": 8.333333333333332, "hits": 1, "name": "vesicle"}]}, {"type": "taxonId", "approximateCount": 1, "values": [{"key": "9606", "percentage": 100.0, "hits": 12, "name": "Homo sapiens"}]}, {"type": "evidenceCode", "approximateCount": 1, "values": [{"key": "ECO:0000314", "percentage": 100.0, "hits": 12, "name": "direct assay evidence used in manual assertion"}]}, {"type": "geneProductId", "approximateCount": 1, "values": [{"key": "UniProtKB:P31749", "percentage": 100.0, "hits": 12, "name": null}]}, {"type": "aspect", "approximateCount": 1, "values": [{"key": "cellular_component", "percentage": 100.0, "hits": 12, "name": null}]}]}, {"groupName": "geneProduct", "totalHits": 1, "types": [{"type": "reference", "approximateCount": 9, "values": [{"key": "GO_REF:0000052", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:19203586", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:20333297", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:10983986", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:14749367", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:16792529", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:20878056", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:21045808", "percentage": 100.0, "hits": 1, "name": null}, {"key": "PMID:25190803", "percentage": 100.0, "hits": 1, "name": null}]}, {"type": "assignedBy", "approximateCount": 3, "values": [{"key": "UniProt", "percentage": 100.0, "hits": 1, "name": null}, {"key": "HPA", "percentage": 100.0, "hits": 1, "name": null}, {"key": "MGI", "percentage": 100.0, "hits": 1, "name": null}]}, {"type": "goId", "approximateCount": 8, "values": [{"key": "GO:0005886", "percentage": 100.0, "hits": 1, "name": "plasma membrane"}, {"key": "GO:0005737", "percentage": 100.0, "hits": 1, "name": "cytoplasm"}, {"key": "GO:0032991", "percentage": 100.0, "hits": 1, "name": "protein-containing complex"}, {"key": "GO:0005634", "percentage": 100.0, "hits": 1, "name": "nucleus"}, {"key": "GO:0005654", "percentage": 100.0, "hits": 1, "name": "nucleoplasm"}, {"key": "GO:0005829", "percentage": 100.0, "hits": 1, "name": "cytosol"}, {"key": "GO:0015630", "percentage": 100.0, "hits": 1, "name": "microtubule cytoskeleton"}, {"key": "GO:0031982", "percentage": 100.0, "hits": 1, "name": "vesicle"}]}, {"type": "taxonId", "approximateCount": 1, "values": [{"key": "9606", "percentage": 100.0, "hits": 1, "name": "Homo sapiens"}]}, {"type": "evidenceCode", "approximateCount": 1, "values": [{"key": "ECO:0000314", "percentage": 100.0, "hits": 1, "name": "direct assay evidence used in manual assertion"}]}, {"type": "geneProductId", "approximateCount": 1, "values": [{"key": "UniProtKB:P31749", "percentage": 100.0, "hits": 1, "name": null}]}, {"type": "aspect", "approximateCount": 1, "values": [{"key": "cellular_component", "percentage": 100.0, "hits": 1, "name": null}]}]}]}
or get "r" like this:
r_ = kscore[0]["r"]
it's says get "r" property from the first elemnt of kscore.
The below code read keys from each type:
with open('kscore.json', 'r') as f:
kscores = json.load(f)
for kscore in kscores:
results = kscore["results"]
for res in results:
types = res["types"]
for type in types:
key = []
values = type["values"]
for val in values:
key.append(val["key"])
print("{} : {} ".format(type["type"], key))
Upvotes: 1