I’m overriding the csv.Dictreader.fieldnames property like the following to read all headers from csv files without white space and in lower case.
import csv
class MyDictReader(csv.DictReader):
@property
def fieldnames(self):
return [field.strip().lower() for field in super(MyDictReader, self).fieldnames]
Now my question is, how can I access the fieldnames with automatically strip() and lower() the query?
This is, how I do it manually:
csvDict = MyDictReader(open('csv-file.csv', 'rU'))
for lineDict in csvDict:
query = ' Column_A'.strip().lower()
print(lineDict[query])
Any ideas?
Based on Pedro Romano’s suggestion I coded the following example.
For a file containing headers like
you can access the columns like this: