不是开箱即用,不。您可以子类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