如何使用Python获取存储在这个json文件中的文件“Path”?

2024-05-05 16:15:32 发布

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

json文件

[{"Attachment": [
{
    "Page:": [
    {
        "Path": "a\\b\\c.pdf",  #field to be extracted
        "PageID": 1
    }
    ]
}
],
"ID": 13221}]

我尝试了以下操作,但得到了TypeError:列表索引必须是整数,而不是str

with open(file) as f:
    d = json.load(f)
print(d[0]['Attachment']['Page']['Path'])

Tags: 文件topathidjsonfield列表attachment
1条回答
网友
1楼 · 发布于 2024-05-05 16:15:32

d[0]['Attachment']是一个列表,d[0]['Attachment'][0]['Page:']也是一个列表。你知道吗

with open(file) as f:
    d = json.load(f)
print(d[0]['Attachment'][0]['Page:'][0]['Path'])

我会做的。你知道吗

相关问题 更多 >