gpt4 book ai didi

python - 类型错误 : string indices must be integers - Parsing JSON

转载 作者:行者123 更新时间:2023-11-30 23:00:39 24 4
gpt4 key购买 nike

我在使用以下 JSON 和读取数据时遇到了一些问题,在查看了其他一些问题后似乎没有找到解决方案,除非我遗漏了一些东西..

总是感谢帮助:)

JSON:

{"ships":{"2":{"name":"Asp","alive":true,"id":2},"3":{"starsystem":{"systemaddress":"670417429889","id":"670417429889","name":"Diaguandri"},"station":{"id":3223343616,"name":"Ray Gateway"},"name":"SideWinder","alive":true,"id":3},"12":{"starsystem":{"systemaddress":"18263140541865","id":"73228","name":"Barnard's Star"},"station":{"id":128147960,"name":"Miller Depot"},"name":"Viper_MkIV","alive":true,"id":12},"13":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"Type7","alive":true,"id":13},"14":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"SideWinder","alive":true,"id":14}}}

Python 代码:

import json

with open('profile.txt') as edstats:
data = json.load(edstats)

def shipYard():
ships = [item["name"] for item in data['ships']]
print json.dumps(ships,indent=4)

错误:

>>> shipYard()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "arg_test_ship.py", line 7, in shipYard
ships = [item["name"] for item in data['ships']]
TypeError: string indices must be integers

最佳答案

您缺少的问题是 data['ships'] 本身就是另一个字典对象。当您像在 ShipYard() 中一样迭代字典时,您将只获得键:

>>> a={'a':1,'b':2}
... [i for i in a]
7: ['a','b']

您想要访问字典内的 name 属性,为此您将使用dictionary.items() 方法:

>>> data = '''{"ships":{"2":{"name":"Asp","alive":true,"id":2},"3":{"starsystem":{"systemaddress":"670417429889","id":"670417429889","name":"Diaguandri"},"station":{"id":3223343616,"name":"Ray Gateway"},"name":"SideWinder","alive":true,"id":3},"12":{"starsystem":{"systemaddress":"18263140541865","id":"73228","name":"Barnard's Star"},"station":{"id":128147960,"name":"Miller Depot"},"name":"Viper_MkIV","alive":true,"id":12},"13":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"Type7","alive":true,"id":13},"14":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"SideWinder","alive":true,"id":14}}}'''
... import json
... data = json.loads(data)
>>> ships = [item['name'] for index, item in data['ships'].items()]
>>> ships
8: [u'Viper_MkIV', u'SideWinder', u'Asp', u'Type7', u'SideWinder']
>>>

或者,如果您不需要索引,请使用字典values()方法:

>>> ships = [item['name'] for item in data['ships'].values()]
>>> ships
9: [u'Viper_MkIV', u'SideWinder', u'Asp', u'Type7', u'SideWinder']
>>>

关于python - 类型错误 : string indices must be integers - Parsing JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com