1

csv.DictReader()默认情况下,将列中的值作为字符串提取。 有没有办法为某些列指定转换?

每次我访问由csv.DictReader()其中包含非字符串元素创建的字典列表时,我最终都会做很多这样的小麻烦:

with open("data.csv","r") as data_file:
    items = csv.DictReader(data_file, fieldnames=('id', 'length', 'note'))
    for item in items:
        item['length'] = float(item['length'])  #### <--- MINOR ANNOYANCE 
        # ... do loop stuff

如果我能告诉csv.DictReader它当它到达某个字段时,它应该进行float()(或int()date())转换会更容易。

4

1 回答 1

3

不是开箱即用,不。您可以子类DictReader()化或创建为您映射行的生成器函数:

def convert_fields(iterable, **conversions):
    for item in iterable:
        for key in item.viewkeys() & conversions:
            item[key] = conversions[key](item[key])
        yield item

对于 Python 3,替换dict.viewkeys()dict.keys(),因为 Python 3 默认返回字典视图。

用它包装你csv.DictReader(),为你的每一列添加转换函数:

with open("data.csv","r") as data_file:
    items = csv.DictReader(data_file, fieldnames=('id', 'length', 'note'))
    items = convert_fields(items, length=float)
    for item in items:
        # item['length'] is now always a float

对于不映射到 python 标识符(带有空格等)的列名,请使用**{..}语法传入字典:

with open("data.csv","r") as data_file:
    items = csv.DictReader(data_file, fieldnames=('id', 'length', 'note'))
    fieldconv = {'id': int, 'length': float, 'spaced column': float}
    items = convert_fields(items, **fieldconv)
    for item in items:
        # item['length'] and item['spaced column'] are now floats
        # item['id'] is always an int
于 2013-09-22T15:39:50.050 回答