【问题标题】:Replace fieldnames when using DictReader使用 DictReader 时替换字段名
【发布时间】:2013-06-07 00:47:37
【问题描述】:

我有一个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"
   }
]

【问题讨论】:

    标签: python json csv etl


    【解决方案1】:

    只需替换这一行:

    reader = csv.DictReader(f, fieldnames = ( "foo","bar","foobar","barfoo" ))
    

    用这个:

    reader = csv.DictReader(f, fieldnames=("id", "email", "name", "phone"))
    

    【讨论】:

    • 请注意,fieldnames 参数为您的数据提供了标头。如果参数被省略,它将取自 csv 文件的第一行。
    【解决方案2】:

    最简单的方法就是设置:

    reader.fieldnames = "email", "name", "id",  "phone"
    

    如果需要,您也可以保存旧的字段名。

    【讨论】:

    • 哦。所以fieldnames不必与CSV中标题行的实际fieldnames对应?
    • @cherrun 不,他们没有。附带说明一下,您甚至不需要像以前那样指定它们。 csv.DictReader(f) 将默认读取第一行作为标题,不需要fieldnames=... 但也许你拥有它的方式更好
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多