Skip to content

Instantly share code, notes, and snippets.

@int3rlop3r
Created August 11, 2016 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save int3rlop3r/0588a79317d2794d7ccbe2cca0752e61 to your computer and use it in GitHub Desktop.
Save int3rlop3r/0588a79317d2794d7ccbe2cca0752e61 to your computer and use it in GitHub Desktop.
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