import csv
import json
# 打开CSV文件并读取数据
with open('data.csv', mode='r', encoding='utf-8') as csvfile:
# CSV文件使用逗号作为分隔符
reader = csv.DictReader(csvfile)
# 将CSV数据转换为字典列表
rows = list(reader)
# 将字典列表转换为JSON格式的字符串
json_string = json.dumps(rows, indent=4)
# 输出JSON字符串或将其写入文件
print(json_string)
# 或者写入到JSON文件
with open('data.json', mode='w', encoding='utf-8') as jsonfile:
jsonfile.write(json_string)
csv转成json python脚本
最新推荐文章于 2024-06-20 21:50:00 发布