从json的多数组中提取数据

2024-09-29 19:19:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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链接中提取“Files”中“OS”中的“Id”字段

我得到的错误如下: TypeError:列表索引必须是整数,而不是str

链接包含以下数据

{ "Files": { "Result": "OK", "Language": [ { "Id": "bg", "Title": "Bulgarian" },

        {
            "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",

        },

    ]
} }

Tags: 数据textimportcomidjsonhttptitle
2条回答

将最后一行代码更改为

打印结果['Files']['OS'][0]['Id']

它将获得操作系统中的第一个id。你知道吗

按以下方式尝试。。。。你知道吗

for i in range(len(result['Files']['OS'])):
   print result['Files']['OS'][i]['Id'];

output
======
001
001
098
0ME
X02
X05

相关问题 更多 >

    热门问题