小编nar*_*ren的帖子

csv到json的转换

我是编程的新手,从过去3/4周开始学习python,这是给出的任务之一.

输入

A, B, C, D
1, 2, 3, 4
5, 6, 7, 8
Run Code Online (Sandbox Code Playgroud)

产量

{{A:"1", B:"2", C:"3", D:"4"}, {A:"5", B:"6", C:"7", D:"8"}}
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用以下代码:

import csv
import json

csvfile = open('test.csv','r')
jsonfile = open('test.json','w')

x = ("a","b","c","d")

reader = csv.DictReader(csvfile, x)
for row in reader:
    json.dump(row, jsonfile)
Run Code Online (Sandbox Code Playgroud)

此代码的输出如下:

{"a": "1", "null": ["5", "6", "7", "8", "9"], "c": "3", "b": "2", "d": "4"}
Run Code Online (Sandbox Code Playgroud)

谁可以帮我这个事?

python csv json

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

csv ×1

json ×1

python ×1