【发布时间】:2014-10-09 12:15:45
【问题描述】:
我是python新手。我需要从json文件中提取数据。
import urllib
import re
import json
text = urllib.urlopen("http://www.acer.com/wjws/ws/gdp/files/en/IN/-/latest/driver/63/-").read()
result = json.loads(text) # result is now a dict
print result['Files']['OS']['Id']
我需要从上面的 JSON 链接中提取“文件”中“操作系统”中的“Id”字段
我收到以下错误: TypeError: 列表索引必须是整数,而不是 str
链接包含的数据为
{ “文件”:{ “结果”:“确定”, “语言”: [ { “Id”:“bg”, “标题”:“保加利亚” },
{ "Id": "no", "Title": "Norwegian" }, ], "SearchedLanguage": "en", "OS": [ { "Id": "001", "Title": "Windows® 2000 Professional" }, { "Id": "098", "Title": "Windows® 98" }, { "Id": "0ME", "Title": "Windows® ME" }, { "Id": "X02", "Title": "Windows® XP 32-bit" }, { "Id": "X05", "Title": "Windows® XP 64-bit" } ], "File": [ { "Link": "http:\/\/global-download.acer.com\/GDFiles\/Driver\/VGA\/VGA_VIA_1.0_w2k.zip?acerid=633676006896131590", "Category": "VGA", }, ] } }
【问题讨论】:
-
OS包含一个列表。您首先必须选择要从中获取Id的列表元素。 -
你能详细说明一下吗。我是新手。你能更正那个代码
-
你有一个字典列表,例如
os = [{'Id': 1}, {'Id': 2}]。您无法访问os['Id'],因为os不是字典,而是列表。您必须首先访问要从中获取 id 的元素,例如os[0]['Id']获取列表第一个元素的 id。您还可以遍历列表以访问每个元素的 id。你还没有真正解释你想要做什么,所以我不知道你在寻找什么解决方案。了解有关列表的更多信息:docs.python.org/2/tutorial/introduction.html#lists、docs.python.org/2/tutorial/datastructures.html -
我需要提取所有 Id 字段值,例如 001,098,X02,X05