预期的结果:
this Code重写似乎是json行
json line构建数据文件到CSV,其中所述的列:
import json
import csv
# Assumptions:
# 1. "Account ID", "AccountName", and "Tags" are keys in every JSON object.
# 2. "Tags" keys are unknown.
# 3. Input file can be completely loaded into memory in order to collect
# other column names from "Tags".
with open('input.jsonl') as file:
columns = ['Account ID', 'AccountName']
records = []
for line in file:
data = json.loads(line)
for d in data['Tags'].copy(): # original data is modified with new columns.
key, value = d['Key'], d['Value']
data[key] = value
if key not in columns:
columns.append(key) # collect "Tag" keys.
del data['Tags']
records.append(data)
with open('output.csv', 'w', encoding='ascii', newline='') as file:
writer = csv.DictWriter(file, fieldnames=columns)
writer.writeheader()
writer.writerows(records)