Created
August 11, 2016 09:13
-
-
Save int3rlop3r/0588a79317d2794d7ccbe2cca0752e61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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().replace(' ', '_') \ | |
for field in csv.DictReader.fieldnames.fget(self)] | |
def next(self): | |
return DictInsensitive(csv.DictReader.next(self)) | |
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().replace(' ', '_')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment