默认情况下,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()
等)转换会更容易。 最佳答案
不是开箱即用的。您可以子类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默认返回字典视图。用包装,为每列添加转换功能:
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标识符(带有空格等)的列名,请使用
csv.DictReader()
语法在字典中传递: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
关于python - csv.DictReader是否可以将某些列设置为字符串以外的格式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18945534/