Is there a possibility to read the header of a CSV file white space and case insensitive? As for now I use csv.dictreader
like this:
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 spacecolumn_A = ' column_A'
elif 'Column_A' in csvDict.fieldnames:# capital Acolumn_A = 'Column_A'# get column_A data
for lineDict in csvDict:print(lineDict[column_A])
As you can see from the code, my csv files sometimes differ in extra white space or capital letters, for example
- "column_A"
- " column_A"
- "Column_A"
- " Column_A"
- ...
I want to use something like this:
column_A = ' Column_A'.strip().lower()print(lineDict[column_A])
Any ideas?