I want to access a specific key value ("cote_1989_base") in Python to modify it. But I have a TypeError: string indices must be integers.
My code:
import json
with open('demo_db_algolia_2019.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
for x in data:
cote1989base = x.get(["cote"][0]["cote_1989"]["cote_1989_eu"]["cote_1989_base"])
print(cote1989base)
EDIT: Maybe I didn't explain this well because my entire JSON is in an array like this:
[{
"objectID": 10035,
"cote":
{
"cote_1989":
{
"cote_1989_f": "750000F",
"cote_1989_eu":
{
"cote_1989_base": 190140
}
},
"cote_2004":
{
"cote_2004_base": 173320
},
"cote_2014":
{
"cote_2014_base": 420800
},
"cote_2017":
{
"cote_2017_base": 939600
},
"cote_2019":
{
"cote_2019_base": 939600
}
}
},
{
"objectID": 10202,
"cote":
{
"cote_1989":
{
"cote_1989_f": "27000F",
"cote_1989_eu":
{
"cote_1989_base": 6844
}
},
"cote_2004":
{
"cote_2004_base": 10894
},
"cote_2014":
{
"cote_2014_base": 23670
},
"cote_2017":
{
"cote_2017_base": 46980
},
"cote_2019":
{
"cote_2019_base": 51156
}
}
}
]
Does it change something to the main problem ?
type(data)to check the type ofdataAttributeError: 'str' object has no attribute 'get'and that is exactly what it does here.["cote"][0]is a string so["cote"][0]["cote_1989"]just does not make sense. What else do you expect?