如何解决在Python中以这种方式打开的CSV文件的文件句柄
#!/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 举报,一经查实,本站将立刻删除。