7

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 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]) 

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?

1
  • If you also want to access the dictionary with automatically strip() and lower() the query, have a look at my fully working code example at stackoverflow.com/a/12970460/1251007 Commented Oct 19, 2012 at 8:45

2 Answers 2

16

You can redefine reader.fieldnames:

import csv import io content = '''column_A " column_B" 1 2''' reader = csv.DictReader(io.BytesIO(content), delimiter = ' ') reader.fieldnames = [field.strip().lower() for field in reader.fieldnames] for line in reader: print(line) 

yields

{'column_b': '2', 'column_a': '1'} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I think your code is more readable/usable for beginners. However, I chose "Defuz" answer as the correct one, as it is more pythonic and can be reused easier.
7

How about override DictReader.fieldnames property?

class MyDictReader(DictReader): @property def fieldnames(self): return [field.strip().lower() for field in super(MyDictReader, self).fieldnames] 

1 Comment

If you try this in Python 2 it won't work because csv.DictReader is a classic-style class and you can't use super in classic-style, stackoverflow.com/questions/9698614/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.