问题描述
#!/usr/bin/env python3
import csv
## Overriding csv.DictReader to strip whitespace and ignore case
class MyDictReader(csv.DictReader,object):
@property
def fieldnames(self):
return [field.strip().lower() for field in super(MyDictReader,self).fieldnames]
# Read csv file
def csv_dict_reader(infile):
reader = MyDictReader(open(infile),delimiter=',',skipinitialspace=True)
return reader
def main():
csvfile = 'mycsvfile.csv'
dict_reader = csv_dict_reader(csvfile)
for row_line in dict_reader:
print(row_line)
if __name__ == '__main__':
main()
为了完整起见,我也添加了csvfile:
sl,applicationid,firstname,lastname
1,Y20QWERT,User1,Name1
2,Y20ZXCVB,User2,Name2
3,Y20WMXK,User3,Name3
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)