【问题标题】:How to get file "Path" stored in this json file using Python?如何使用 Python 获取存储在此 json 文件中的文件“路径”?
【发布时间】:2018-11-09 18:41:40
【问题描述】:

json 文件

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

我尝试了以下但得到 TypeError: list indices must be integers, not str

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

【问题讨论】:

    标签: python json


    【解决方案1】:

    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'])
    

    会做的。

    【讨论】:

    • 抱歉,在“Page”之后没有看到“:”,请参阅编辑后的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多