繁体   English   中英

通过合并文本重新格式化 Python 中的 JSON 文件?

[英]Reformat JSON file in Python by consolidating text?

我有以下格式的 JSON 文件:

[
{"url": "example1.com", "date": "Jan 1", "text": ["Here is some ", "example 1 text"]},
{"url": "example2.com", "date": "Jan 2", "text": ["Here is some ", "example 2 text"]},
]

我使用以下方式上传到 Python:

with open("data.json") as data:
    data = json.load(data)

我想重新格式化上传的数据,以便合并文本而不用括号括起来,如下所示:

[
{"url": "example1.com", "date": "Jan 1", "text": "Here is some example 1 text"},
{"url": "example2.com", "date": "Jan 2", "text": "Here is some example 2 text"},
]

您可以按如下方式格式化“文本”字段:

with open("data.json") as data:
    data = json.load(data)
    for website in data:
        website["text"] = ' '.join(website["text"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM