1

我正在覆盖如下csv.Dictreader.fieldnames属性,以读取 csv 文件中的所有标题,不带空格且为小写。

import csv
class MyDictReader(csv.DictReader):

    @property
    def fieldnames(self):
        return [field.strip().lower() for field in super(MyDictReader, self).fieldnames]

现在我的问题是,如何自动访问字段名strip()lower()查询?

这就是我如何手动操作:

csvDict = MyDictReader(open('csv-file.csv', 'rU'))

for lineDict in csvDict:
    query = ' Column_A'.strip().lower()
    print(lineDict[query])

有任何想法吗?

4

2 回答 2

2

根据 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
于 2012-10-19T08:42:03.557 回答
1

您必须分两步完成:

  1. 使用将 应用于其参数的方法创建您的dict专业化。__getitem__.strip().lower()key
  2. 覆盖__next__您的MyDictReader专用类以返回使用csv.DictReader超类__next__方法返回的字典初始化的特殊字典之一。
于 2012-10-18T17:41:13.393 回答