我有一些带有“项目列表”的 JSON。我想将其转换为带标题的分号分隔文件
我想以这种格式生成线条:
id, date, order[x][0], order[x][1], ...
我的问题是我需要包含标题、字段名称,并且一些
order
项目没有所有可能的元素,因此我不能只是将这些值连接在一起。
对于下面的 JSON,我期望以下输出:
id;date;prefix;number;quantity;code;index
06107;2023-09-25T01:51:04Z;VO;32233809;1;;4
06107;2023-09-25T01:51:04Z;VO;31438125;1;;4
06107;2023-09-25T10:00:51Z;VO;31407983;1;14;4
06107;2023-09-25T10:00:51Z;VO;986116;6;12;4
我猜也许某种熊猫用法是解决方案,但我不确定如何进行映射和导出。
"items": [
{
"id": "06107",
"date": "2023-09-25T01:51:04Z",
"order": [
{
"prefix": "VO",
"number": "32233809",
"quantity": 1,
"index": 4
},
{
"prefix": "VO",
"number": "31438125",
"quantity": 1,
"index": 4
}
]
},
{
"id": "06107",
"date": "2023-09-25T10:00:51Z",
"order": [
{
"prefix": "VO",
"number": "31407983",
"quantity": 1,
"code": 14,
"index": 4
},
{
"prefix": "VO",
"number": "986116",
"quantity": 6,
"code": 12,
"index": 4
}
]
}
]
我认为最简单的方法是通过
csv.DictWriter()
extrasaction="ignore"
。
import csv
data = [
{
"id": "06107",
"date": "2023-09-25T01:51:04Z",
"order": [
{"prefix": "VO", "number": "32233809", "quantity": 1, "index": 4},
{"prefix": "VO", "number": "31438125", "quantity": 1, "index": 4}
]
},
{
"id": "06107",
"date": "2023-09-25T10:00:51Z",
"order": [
{"prefix": "VO", "number": "31407983", "quantity": 1, "code": 14, "index": 4},
{"prefix": "VO", "number": "986116", "quantity": 6, "code": 12, "index": 4}
]
}
]
all_items = list()
for item in data:
for detail in item["order"]:
all_items.append({
**{"id": item["id"], "date": item["date"]},
**detail
})
headers = ["id", "date", "prefix", "number", "quantity", "code", "index"]
with open("foo.csv", "w", newline="") as file_out:
writer = csv.DictWriter(file_out, delimiter=";", fieldnames=headers, extrasaction="ignore")
writer.writeheader()
writer.writerows(all_items)
这会给你一个文件:
id;date;prefix;number;quantity;code;index
06107;2023-09-25T01:51:04Z;VO;32233809;1;;4
06107;2023-09-25T01:51:04Z;VO;31438125;1;;4
06107;2023-09-25T10:00:51Z;VO;31407983;1;14;4
06107;2023-09-25T10:00:51Z;VO;986116;6;12;4