簡體   English   中英

使用 python 從 json 文件中讀取元素

[英]read the element from json file with python

我是新來閱讀 python 中的 json 文件。 我想從文件中獲取 url。 這是我的 json 文件。

[
    {
        "author": "[{'name': 'Ahmed Osman'}, {'name': 'Wojciech Samek'}]",
        "day": 1,
        "id": "1802.00209v1",
        "link": "[{'rel': 'alternate', 'href': 'http://arxiv.org/abs/1802.00209v1', 'type': 'text/html'}, {'rel': 'related', 'href': 'http://arxiv.org/pdf/1802.00209v1', 'type': 'application/pdf', 'title': 'pdf'}]",
        "month": 2,
        "summary": "We propose an architecture for VQA which utilizes recurrent layers to\ngenerate visual and textual attention. The memory characteristic of the\nproposed recurrent attention units offers a rich joint embedding of visual and\ntextual features and enables the model to reason relations between several\nparts of the image and question. Our single model outperforms the first place\nwinner on the VQA 1.0 dataset, performs within margin to the current\nstate-of-the-art ensemble model. We also experiment with replacing attention\nmechanisms in other state-of-the-art models with our implementation and show\nincreased accuracy. In both cases, our recurrent attention mechanism improves\nperformance in tasks requiring sequential or relational reasoning on the VQA\ndataset.",
        "tag": "[{'term': 'cs.AI', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.CL', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.CV', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.NE', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'stat.ML', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}]",
        "title": "Dual Recurrent Attention Units for Visual Question Answering",
        "year": 2018
    },
    {
        "author": "[{'name': 'Ji Young Lee'}, {'name': 'Franck Dernoncourt'}]",
        "day": 12,
        "id": "1603.03827v1",
        "link": "[{'rel': 'alternate', 'href': 'http://arxiv.org/abs/1603.03827v1', 'type': 'text/html'}, {'rel': 'related', 'href': 'http://arxiv.org/pdf/1603.03827v1', 'type': 'application/pdf', 'title': 'pdf'}]",
        "month": 3,
        "summary": "Recent approaches based on artificial neural networks (ANNs) have shown\npromising results for short-text classification. However, many short texts\noccur in sequences (e.g., sentences in a document or utterances in a dialog),\nand most existing ANN-based systems do not leverage the preceding short texts\nwhen classifying a subsequent one. In this work, we present a model based on\nrecurrent neural networks and convolutional neural networks that incorporates\nthe preceding short texts. Our model achieves state-of-the-art results on three\ndifferent datasets for dialog act prediction.",
        "tag": "[{'term': 'cs.CL', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.AI', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.LG', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'cs.NE', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}, {'term': 'stat.ML', 'scheme': 'http://arxiv.org/schemas/atom', 'label': None}]",
        "title": "Sequential Short-Text Classification with Recurrent and Convolutional\n  Neural Networks",
        "year": 2016
    }
]

我使用以下代碼讀取文件。

with open(args.filename, 'r') as myfile:
    data = json.loads(myfile.read())
    myfile.close()

我想通過使用data[0]["link"][1]["href"]來獲得第二個href 但是data[0]["link"]的類型是字符串。 我想知道我該如何處理這個問題。

您可以在 json 中使用ast.eval_literal()來制作“字符串格式化”列表,將其解釋為列表,然后按照您正確的說明引用它。

從您的數據開始,這對我有用:

import ast
print(ast.literal_eval(data[0]['link'])[1]['href'])

Output:

http://arxiv.org/pdf/1802.00209v1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM