Access to csv header empty space and case insensitive

I override the csv.Dictreader.fieldnames property as follows to read all headers from csv files without spaces and lowercase.

 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 field names using automatically strip() and lower() 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?

+1
source share
2 answers

Based on the suggestion of Pedro Romano, I coded the following example.

 import csv class DictReaderInsensitive(csv.DictReader): # This class overrides the csv.fieldnames property. # All fieldnames are without white space and in lower case @property def fieldnames(self): return [field.strip().lower() for field in super(DictReaderInsensitive, self).fieldnames] def __next__(self): # get the result from the original __next__, but store it in DictInsensitive dInsensitive = DictInsensitive() dOriginal = super(DictReaderInsensitive, self).__next__() # store all pairs from the old dict in the new, custom one for key, value in dOriginal.items(): dInsensitive[key] = value return dInsensitive class DictInsensitive(dict): # This class overrides the __getitem__ method to automatically strip() and lower() the input key def __getitem__(self, key): return dict.__getitem__(self, key.strip().lower()) 

For a file containing type headers

  • "column_A"
  • "column_A"
  • "Column_A"
  • "Column_A"
  • ...

you can access the columns as follows:

 csvDict = DictReaderInsensitive(open('csv-file.csv', 'rU')) for lineDict in csvDict: print(lineDict[' Column_A']) # or print(lineDict['Column_A']) # or print(lineDict[' column_a']) # all returns the same 
+2
source

You need to do this in two steps:

  • Create your dict specialization using the __getitem__ method, which applies .strip().lower() to its key parameter.
  • Override __next__ in the specialized MyDictReader class to return one of your special dictionaries, initialized by the dictionary returned by the csv.DictReader <class> tcl> __next__ .
+1
source

All Articles