【问题标题】:Extract titles and paragraphs from json file by HTML elements style with keys:value both通过带有键的 HTML 元素样式从 json 文件中提取标题和段落:值都
【发布时间】:2021-11-11 10:58:39
【问题描述】:

在一个 json 文件中有许多标题和段落以及许多字体。我想从json文件中提取所有带有keys:value的标题和段落,并使用python重新写入newJson文件。

在许多地方,'Text' 键不在 json 文件中。我想写'路径'和'文本'两个键:值。对象中是否存在 我想这样做,但它不会发生。

import json
keyword = ("Path", "Text")
with open("structuredData.json", encoding="utf8") as f:
    json_dict = json.load(f)
    a=json_dict['elements'][1]['Path']
    b=json_dict['elements'][1]['Text']
with open("indra5.json", 'w') as f:
f.write(a+"\n" )
f.write(b+"\n")

这是我拍摄并上传的一张截图。在我给出分拣机的许多行中。 在该图像中,我想使用其键提取“路径”和“文本”:值

【问题讨论】:

    标签: json file extract paragraph


    【解决方案1】:

    试试这个,记住 .json 文件的格式是 [{"key": "value"}, {"key": "value"}]

    import json
    
    with open("structuredData.json", encoding="utf8") as f:
        json_dict = json.load(f)
        with open("indra5.json", 'w') as f2:
            f2.write("[\n")
            for element in json_dict:
                f2.write("{\n")
                f2.write('"path":"'+element["path"]+'",\n')
                f2.write('"text":"'+element["text"]+'"\n')
                f2.write("},\n")
            f2.write("]\n")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2014-04-21
      相关资源
      最近更新 更多