csv转json
csv 文件示例
dishes_id,dish_name,早餐,中餐,晚餐,加餐,米饭,面食或面点,粗粮,热菜,凉菜,汤,点心,蒸,炒,烧,炖,烩,煨,煎,烤,卤,焖,炸,灼,鲁菜,川菜,粤菜,江苏菜,闽菜,浙江菜,湘菜,徽菜,东北菜,新疆菜,淮扬菜,晋菜,云南菜,西北菜,潮州菜,其他地区特色,酸,甜,苦,辣,咸,水果,乳制品,主食,坚果,面片与挂面,馄饨,蒸或煮的鸡蛋,可煮烂的肉菜,可滤渣的蔬菜汁,白色意大利面,精面做的面包和谷物,牛奶,豆浆,含蔗糖的食物,粥
969855,猪脚姜,0.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
2350698,苹果醋泡藕片,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0
2396213,豉油鸡,0.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
777765,橙蜜莲藕,1.0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0
json文件示例
{
"total_count": 1000,
"dishes_list": [{
"dish_id": 3000281,
"dish_name": "米饭",
"早餐": 0,
"中餐": 1,
"晚餐": 1,
"加餐": 0,
"米饭": 1,
"面食或面点": 0,
"粗粮": 0,
"热菜": 0,
"凉菜": 0,
"汤": 0,
"点心": 0,
"蒸": 0,
......
}]
}
工程结构
完整代码
import json
data_file = "douguo_labels.csv"
target_file = "test.txt"
with open(data_file, 'r', encoding='utf8') as file:
file_list = file.readlines()
row_count = len(file_list)
header = file_list[0].split(",")
col_count = len(header)
sub_dict = {}
dishes_list = []
for i in range(row_count):
if i > 0:
line_list = file_list[i].split(",")
for j in range(col_count):
if j == 1:
sub_dict.update({header[j]: str(line_list[j])})
else:
sub_dict.update({header[j]: int(float(line_list[j]))})
dishes_list.append(sub_dict)
result_dict = {
"total_count": row_count,
"dishes_list": dishes_list
}
with open(target_file, 'w', encoding='utf8') as target_file:
result = json.dumps(result_dict, ensure_ascii=False, indent=4, separators=(',', ':'))
target_file.write(result)
json转CSV
json文件示例
{
"pdtBeginTime": "2022-09-15 05:37:42",
"data": [
{
"machine_code": "Blower",
"machine_name": "吹瓶机",
"realPdtList": [
{
"beginTime": "2022-09-15 05:37:42",
"endTime": "2022-09-15 05:38:08",
"status": "self",
"timeLen": "00:00:26"
},
{
"beginTime": "2022-09-15 05:38:08",
"endTime": "2022-09-15 05:38:49",
"status": "normal",
"timeLen": "00:00:41"
}
]
}
],
"pdtEndTime": "2022-09-15 05:38:49",
"realLineCode": "BJP8",
"status": "success"
}
csv 文件示例
pdtBeginTime,pdtEndTime,realLineCode,machine_code,machine_name,beginTime,endTime,status,timeLen
2022-09-15 05:37:42,2022-09-15 05:38:49,BJP8,Blower,吹瓶机,2022-09-15 05:38:08,2022-09-15 05:38:49,normal,00:00:41
2022-09-15 05:37:42,2022-09-15 05:38:49,BJP8,Blower,吹瓶机,2022-09-15 05:38:08,2022-09-15 05:38:49,normal,00:00:41
工程目录
完整代码
import json
import pandas
table_list = []
with open('test.txt', 'r', encoding='utf8') as file:
context = file.read()
context_dict = json.loads(context)
row_dict = {}
row_dict.update({'pdtBeginTime': context_dict['pdtBeginTime']})
row_dict.update({'pdtEndTime': context_dict['pdtEndTime']})
row_dict.update({'realLineCode': context_dict['realLineCode']})
data_list = context_dict['data']
for machine in data_list:
row_dict.update({'machine_code': machine['machine_code']})
row_dict.update({'machine_name': machine['machine_name']})
realPdtList = machine['realPdtList']
for realPdt in realPdtList:
row_dict.update({'beginTime': realPdt['beginTime']})
row_dict.update({'endTime': realPdt['endTime']})
row_dict.update({'status': realPdt['status']})
row_dict.update({'timeLen': realPdt['timeLen']})
table_list.append(row_dict)
df = pandas.DataFrame(table_list)
df.to_csv('test.csv', index=None)