Pedro Romano の提案に基づいて、次の例をコーディングしました。
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())
次のようなヘッダーを含むファイルの場合
- "列_A"
- "列_A"
- 「列A」
- 「列A」
- ...
次のように列にアクセスできます。
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