将数组结构的组件作为Python

问题描述 投票:0回答:1

预期的结果:

    

this Code重写似乎是json行

json line

构建数据文件到CSV,其中所述的列:Table of data

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)
python json dataframe aws-lambda struct
1个回答
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.