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()