是否有可能读取 CSV 文件的标题空白和不区分大小写?至于现在我csv.dictreader
这样使用:
import csv
csvDict = csv.DictReader(open('csv-file.csv', 'rU'))
# determine column_A name
if 'column_A' in csvDict.fieldnames:
column_A = 'column_A'
elif ' column_A' in csvDict.fieldnames:
# extra space
column_A = ' column_A'
elif 'Column_A' in csvDict.fieldnames:
# capital A
column_A = 'Column_A'
# get column_A data
for lineDict in csvDict:
print(lineDict[column_A])
从代码中可以看出,我的 csv 文件有时会在额外的空格或大写字母方面有所不同,例如
- “列_A”
- “列_A”
- “列_A”
- “列_A”
- ...
我想使用这样的东西:
column_A = ' Column_A'.strip().lower()
print(lineDict[column_A])
有任何想法吗?