'python how to trim trailing spaces in csv DictReader keys
I am using python (2.6) csv DictReader. My input file has a header line where the column names have trailing spaces:
colname1, colname2 ,col3, etc.
XX, YY, ZZ
The returned dict object has key() = ['colname1', 'colname2 ', 'col3']
Is there an option to trim leading and trailing spaces from the keys?
--edit
The problem arises in processing by key names:
with open(fname) as f:
r = csv.DictReader(f)
for row in r:
print "processing", r["column1"], r["column2"]
The files are database dumps. And the dump program is way too smart - it adjust the output column width depending on data -- which means different sets of selects are going to have different column width and different key lengths. Sometimes I must use r['column2 ']
and sometimes pad or reduce spaces. ouch!
Solution 1:[1]
You need to register a custom dialect in the csv module
csv.register_dialect('MyDialect', quotechar='"', skipinitialspace=True, quoting=csv.QUOTE_NONE, lineterminator='\n', strict=True)
then use the dialect when creating the DictReader:
my_reader = csv.DictReader(trip_file, dialect='MyDialect')
Here's all the Dialect Options
Solution 2:[2]
Python3 version
with open('file.csv') as fh:
header = [h.strip() for h in fh.readline().split(',')]
reader = csv.DictReader(fh, fieldnames=header)
Solution 3:[3]
Following in the vein of other answers, but why not use the CSV reader for that header row?
header = [h.strip() for h in next(csv.reader(f))]
reader = csv.DictReader(f, fieldnames=header)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | klucar |
Solution 2 | Luis |
Solution 3 | Kevin |