Accessing csv header white space and case insensitive

前端 未结 2 1621
予麋鹿
予麋鹿 2021-01-23 13:44

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.

         


        
相关标签:
2条回答
  • 2021-01-23 14:04

    Based on Pedro Romano's suggestion 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 headers like

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

    you can access the columns like this:

    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
    
    0 讨论(0)
  • 2021-01-23 14:14

    You'll have to do it in two steps:

    1. Create your dict specialisation with a __getitem__ method that applies the .strip().lower() to the its key parameter.
    2. Override __next__ on your MyDictReader specialised class to return one of your special dictionaries initialised with the dictionary returned by the csv.DictReader superclass's __next__ method.
    0 讨论(0)
提交回复
热议问题