解析 JSON - python 时字符串索引必须是整数
string indices must be integers while parsing JSON - python
我是 python 的新手,在解析 JSON 文件时遇到此错误:string indices must be integers
。
JSON 文件:
{"NFLTeams": [
{"code":"ARI","fullName":"Arizona Cardinals","shortName":"Arizona"},
{"code":"ATL","fullName":"Atlanta Falcons","shortName":"Atlanta"},
{"code":"WAS","fullName":"Washington Redskins","shortName":"Washington"}
]}
我的代码:
import urllib.request as ur
import urllib.parse
import json
url = 'http://www.fantasyfootballnerd.com/service/nfl-teams/json/test/'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read().decode('utf-8')
print (the_page)
team_data = json.loads(the_page)
print (type(team_data)) // team_data is type dict
for item in team_data:
print (item['NFLTeams'][0]["code"])
print (item['NFLTeams'][0]['fullName'])
print (item['NFLTeams'][0]['shortName'])
我尝试打印以下内容:
print (item['NFLTeams']["code"])
还有这个:
for item in team_data['NFLTeams'].values():
print (item["code"])
print (item['fullName'])
print (item['shortName'])
这给了我这个错误:
for item in team_data['NFLTeams'].values():
AttributeError: 'list' object has no attribute 'values'
谁能帮我弄清楚这是怎么回事?谢谢。
.values()
用于遍历字典的值,但是,team_data['NFLTeams']
是包含字典的 list
。因此,您需要删除 .values()
以在迭代时访问每个字典:
for item in team_data['NFLTeams']:
print (item["code"])
print (item['fullName'])
print (item['shortName'])
如果你真的想用.values()
:
for item in team_data.values()[0]:
print (item["code"])
print (item['fullName'])
print (item['shortName'])
请记住 .values()
returns view object in Python 3.x,因此您需要使用 [=19= 强制评估它] 为了按索引访问元素:
for item in list(team_data.values())[0]:
我是 python 的新手,在解析 JSON 文件时遇到此错误:string indices must be integers
。
JSON 文件:
{"NFLTeams": [
{"code":"ARI","fullName":"Arizona Cardinals","shortName":"Arizona"},
{"code":"ATL","fullName":"Atlanta Falcons","shortName":"Atlanta"},
{"code":"WAS","fullName":"Washington Redskins","shortName":"Washington"}
]}
我的代码:
import urllib.request as ur
import urllib.parse
import json
url = 'http://www.fantasyfootballnerd.com/service/nfl-teams/json/test/'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read().decode('utf-8')
print (the_page)
team_data = json.loads(the_page)
print (type(team_data)) // team_data is type dict
for item in team_data:
print (item['NFLTeams'][0]["code"])
print (item['NFLTeams'][0]['fullName'])
print (item['NFLTeams'][0]['shortName'])
我尝试打印以下内容:
print (item['NFLTeams']["code"])
还有这个:
for item in team_data['NFLTeams'].values():
print (item["code"])
print (item['fullName'])
print (item['shortName'])
这给了我这个错误:
for item in team_data['NFLTeams'].values():
AttributeError: 'list' object has no attribute 'values'
谁能帮我弄清楚这是怎么回事?谢谢。
.values()
用于遍历字典的值,但是,team_data['NFLTeams']
是包含字典的 list
。因此,您需要删除 .values()
以在迭代时访问每个字典:
for item in team_data['NFLTeams']:
print (item["code"])
print (item['fullName'])
print (item['shortName'])
如果你真的想用.values()
:
for item in team_data.values()[0]:
print (item["code"])
print (item['fullName'])
print (item['shortName'])
请记住 .values()
returns view object in Python 3.x,因此您需要使用 [=19= 强制评估它] 为了按索引访问元素:
for item in list(team_data.values())[0]: