python 多層嵌套json文件轉csv文件


import csv
import json
import jsonpath
import pandas as pd
import time

json_file1 = open(r'C:\Users\ellzhang\Desktop\API-返回-11號訂單.json', 'r', encoding='utf_8')
csv_file1 = open(r'C:\Users\ellzhang\Desktop\API-返回-11號訂單2.csv', 'w', newline='',encoding='utf_8_sig')
keys = []
keys_2 = []
key_prod = []
dic_all = {}
writer1 = csv.writer(csv_file1)
json_data1 = json_file1.read()
dic_data = json.loads(json_data1, encoding='utf_8_sig')
tmp = jsonpath.jsonpath(dic_data,'$..dataList')
ls_all = tmp[0]
for dic in ls_all:
    #print(type(dic)) dict
        keys = list(dic.keys())
        #print(keys)
        keys.remove('productSkulList')
        #print(keys)
        keys_2 = list(dic.keys())
        #keys_2.remove('productSkulList')
        #print(keys)
        ls_p = list(dic.values())[15][0]
        key_prod = list(ls_p.keys())
        print(keys_2)
        keys_2[15:16] = key_prod
        print(keys_2)

        # 寫入列名
        writer1.writerow(keys_2)
        break
        #print(keys)

def get_target_value(key, dic, tmp_list):
    """
    :param key: 目標key值
    :param dic: JSON數據
    :param tmp_list: 用於存儲獲取的數據
    :return: list
    """
    if not isinstance(dic, dict) or not isinstance(tmp_list, list):  # 對傳入數據進行格式校驗
        return 'argv[1] not an dict or argv[-1] not an list '

    if key in dic.keys():
        tmp_list.append(dic[key])  # 傳入數據存在則存入tmp_list


    for value in dic.values():  # 傳入數據不符合則對其value值進行遍歷
        if isinstance(value, dict):
            get_target_value(key, value, tmp_list)  # 傳入數據的value值是字典,則直接調用自身
        elif isinstance(value, (list, tuple)):
            _get_value(key, value, tmp_list)  # 傳入數據的value值是列表或者元組,則調用_get_value


    return tmp_list


def _get_value(key, val, tmp_list):
    for val_ in val:
        if isinstance(val_, dict):  
            get_target_value(key, val_, tmp_list)  # 傳入數據的value值是字典,則調用get_target_value
        elif isinstance(val_, (list, tuple)):
            _get_value(key, val_, tmp_list)   # 傳入數據的value值是列表或者元組,則調用自身
#不包括嵌套內容的文件
def trans_1():


    for key in keys:
        
        ls2 = []
        for ls in ls_all:

            ls1 = get_target_value(key,ls,[])
            if len(ls1) != 0:
                if key == 'createTime':
                    time_local = time.localtime(int(str(ls1[0])[0:10]))
                    dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
                    ls2.append(dt)
                else:
                    ls2.append(ls1[0])
            else :
                ls2.append(0)
        dic_all[key] = ls2
        

                #print(ls1)
            
    #print(dic_all)
    df = pd.DataFrame.from_dict(dic_all)
    df.to_csv(r'C:\Users\ellzhang\Desktop\API-返回-11號訂單1.csv',index = False,encoding='utf_8_sig')
#包括嵌套內容的文件
def trans_2():
    for ls in ls_all:
        ind = list(ls.keys()).index('productSkulList')

        ls_p = list(ls.values())[ind][0]
        key_prod = list(ls_p.keys())
        
        len_prod = len(list(ls.values())[ind])
        #print(len_prod)
        for m in range(len_prod):
            ls_tmp  = []
            #print(m)
            for key in keys:
            
                if key == 'shopCode':#嵌套字段的前一個字段
                    ls1 = get_target_value(key,ls,[])
                    if len(ls1) != 0:
                        ls_tmp.append(ls1[0])
                    else:
                        ls_tmp.append(0)
                    
                    
                    for key_p in key_prod:
                        #print(list(ls.values())[15][m])
                        ls1=get_target_value(key_p,list(ls.values())[ind][m],[])
                        if len(ls1) != 0:
                            ls_tmp.append(ls1[0])
                        else:
                            ls_tmp.append(0)

                else:
                    ls1 = get_target_value(key,ls,[])
                    if len(ls1) != 0:
                        if key == 'createTime':#UNIX時間轉標準日期格式
                            time_local = time.localtime(int(str(ls1[0])[0:10]))
                            dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
                            #print(dt)
                            ls_tmp.append(str(dt))
                        else:
                            ls_tmp.append(ls1[0])
                    else:
                        ls_tmp.append(0)

            writer1.writerow(ls_tmp)
if __name__ =='__main__':
    trans_1()
    trans_2()
    

    
    
    
        
           

發佈了10 篇原創文章 · 獲贊 1 · 訪問量 2288
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章