簡體   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