50csv表格转换为json文件

 

import csv
import json
# 常规csv表格转换为json文件,表头作为字典key字段。

def convert_csv_to_json(csv_file_path, json_file_path):
    data = []
    with open(csv_file_path, 'r',encoding='utf-8-sig') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        for row in csv_reader:
            data.append(row)
    print(data)
    with open(json_file_path, 'w',encoding='utf-8-sig') as json_file:
        json.dump(data, json_file, indent=4,ensure_ascii=False)

csv_file_path = 'input.csv'
json_file_path = 'ouput.json'
convert_csv_to_json(csv_file_path, json_file_path)

 

posted @ 2024-01-21 15:16  冷夜O  阅读(11)  评论(0)    收藏  举报