我有test.csv
个文件:
foo,bar,foobar,barfoo
1,2,3,4
5,6,7,8
9,10,11,12
以下CSV
解析器:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import json
f = open ( 'test.csv', 'r' )
reader = csv.DictReader( f, fieldnames = ( "foo","bar","foobar","barfoo" ))
out = json.dumps( [ row for row in reader ], ensure_ascii=False, encoding="utf-8")
print out
是否有一种简单的方法可以替换输出中的字段名,而无需更改CSV
文件的标题?
我目前的输出是:
[
{
"foobar":"foobar",
"foo":"foo",
"bar":"bar",
"barfoo":"barfoo"
},
{
"foobar":"3",
"foo":"1",
"bar":"2",
"barfoo":"4"
},
{
"foobar":"7",
"foo":"5",
"bar":"6",
"barfoo":"8"
},
{
"foobar":"11",
"foo":"9",
"bar":"10",
"barfoo":"12"
}
]
我可以得到这样的东西:
[
{
"id":"foobar",
"email":"foo",
"name":"bar",
"phone":"barfoo"
},
{
"id":"3",
"email":"1",
"name":"2",
"phone":"4"
},
{
"id":"7",
"email":"5",
"name":"6",
"phone":"8"
},
{
"id":"11",
"email":"9",
"name":"10",
"phone":"12"
}
]
答案 0 :(得分:9)
最简单的方法是设置:
reader.fieldnames = "email", "name", "id", "phone"
如果您愿意,也可以保存旧的字段名。
答案 1 :(得分:4)
只需替换此行:
reader = csv.DictReader(f, fieldnames = ( "foo","bar","foobar","barfoo" ))
用这个:
reader = csv.DictReader(f, fieldnames=("id", "email", "name", "phone"))