将 JSON 字典文件转换为 CSV

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

需要提取recordID、日期、标题和突破action_history数据。 由于 action_history 数据也具有 recordID,因此它不必全部位于 CSV 文件的同一行上。 这是 JSON 字符串:

{
"783": {        
        "recordID": 783,
        "title": "Test1",
        "date": 1723572991,        
        "action_history": [
            {
                "recordID": 783,               
                "time": 1723573283,                
                "actionType": "submit"               
            },           
            {
                "recordID": 783,               
                "time": 1723573425,               
                "actionType": "Save"
            },
            {
                "recordID": 783,               
                "time": 1723585061,
                "actionType": "Complete"
            }
        ]       
    },
"900": {        
        "recordID": 900,
        "title": "Test2",
        "date": 1723572825,        
        "action_history": [
            {
                "recordID": 900,               
                "time": 1723573300,                
                "actionType": "submit"               
            },           
            {
                "recordID": 900,               
                "time": 1723573350,               
                "actionType": "Save"
            },
            {
                "recordID": 900,               
                "time": 1723585390,
                "actionType": "Complete"
            }
        ]       
    }
}

仅使用基本的

json.load
csv
转换方法还没有走得太远。 使用此 JSON 结构,我在分解每个 recordID 的 action_history 时遇到困难。由于已指示 recordID,action_history 可以位于不同的行中(我可以稍后加入记录)。

文件输出:

recordID,title,date,action_history
783,Test1,1723572991,"[{'recordID': 783, 'time': 1723573283, 'actionType': 'submit'}, {'recordID': 783, 'time': 1723573425, 'actionType': 'Save'}, {'recordID': 783, 'time': 1723585061, 'actionType': 'Complete'}]"
900,Test2,1723572825,"[{'recordID': 900, 'time': 1723573300, 'actionType': 'submit'}, {'recordID': 900, 'time': 1723573350, 'actionType': 'Save'}, {'recordID': 900, 'time': 1723585390, 'actionType': 'Complete'}]"

脚本:

import csv
import json

def json_to_csv(json_file, csv_file):

    with open(json_file) as f:
        data = json.load(f)

        f = csv.writer(open(csv_file, "w+", newline=""))

        f.writerow(["recordID","title","date","action_history"])
        for x in data.values():
            f.writerow([x["recordID"],
                        x["title"],
                        x["date"],
                        x["action_history"]])

#src, dest, function call
json_file = 'source.json'
csv_file = 'output.csv'
json_to_csv(json_file, csv_file)
python json csv
1个回答
0
投票

你可以尝试:

import csv
import json

def json_to_csv(json_file, csv_file):

    with open(json_file) as f:
        data = json.load(f)

        f = csv.writer(open(csv_file, "w+", newline=""))
        header = ["recordID","title","date","action_history",
                  "action_Submit_time",
                  "action_Save_time",
                  "action_Complete_time"
                  ]
        f.writerow(header)
        for x in data.values():            
            result = [x["recordID"],
                    x["title"],
                    x["date"],
                ]
            
            result.extend( [ j["time"] for j in  x["action_history"] ]) 
                
            f.writerow(result)

#src, dest, function call
json_file = 'source.json'
csv_file = 'output.csv'
json_to_csv(json_file, csv_file)
© www.soinside.com 2019 - 2024. All rights reserved.