我有以下格式的 JSON 檔案:
[
{"url": "example1.com", "date": "Jan 1", "text": "Example text 1"},
{"url": "example2.com", "date": "Jan 2", "text": "Example text 2"}
]
我使用以下方法上傳到 Python 中:
with open("data.json") as data:
data = json.load(data)
我想將上傳的資料重新格式化為以下格式:
[
(
"Example text 1",
{"url": "example1.com", "date": "Jan 1"}
),
(
"Example text 2",
{"url": "example2.com", "date": "Jan 2"}
)
]
uj5u.com熱心網友回復:
嘗試這個
data = [
{"url": "example1.com", "date": "Jan 1", "text": "Example text 1"},
{"url": "example2.com", "date": "Jan 2", "text": "Example text 2"}
]
# make tuples
[(d.pop('text'), d) for d in data]
# [('Example text 1', {'url': 'example1.com', 'date': 'Jan 1'}),
# ('Example text 2', {'url': 'example2.com', 'date': 'Jan 2'})]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/487364.html
下一篇:檔案頂部帶有附加內容的JSON