【问题标题】:Python dictreader - How to make CSV column names lowercase?Python dictreader - 如何使 CSV 列名小写?
【发布时间】:2013-06-01 00:10:39
【问题描述】:

我有一个列名大写的 CSV 文件。我正在使用 csv.dictreader 读取数据,但需要小写的列名。

我在这里找到了这段代码Accessing csv header white space and case insensitive

    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())

我的问题是当我用

运行它时
datafile = open(self.ifs_data_file,'rU')
        csvDict = DictReaderInsensitive(datafile)
        for row in csvDict:
            print row
            #self.db.ifs_data.insert(**row)
            #self.db.commit()

我收到此错误

Traceback (most recent call last):
  File "D:\Development\python\supplier_review\supplier_review.py", line 239, in update_ifs_data
    for row in csvDict:
  File "D:\Python27_5\lib\csv.py", line 103, in next
    self.fieldnames
  File "D:\Development\python\supplier_review\supplier_review.py", line 288, in fieldnames
    return [field.strip().lower() for field in super(DictReaderInsensitive, self).fieldnames]
TypeError: must be type, not classobj

【问题讨论】:

    标签: python python-2.7 csv


    【解决方案1】:

    您可以将文件的第一行小写,然后再将其传递给DictReader

    import csv
    import itertools
    
    def lower_first(iterator):
        return itertools.chain([next(iterator).lower()], iterator)
    
    with open(ifs_data_file, 'rU') as datafile:
        csvDict = csv.DictReader(lower_first(datafile))
        for row in csvDict:
            print row    
    

    【讨论】:

    • 四年后,这仍然是一种有用且易于实施的技术。
    【解决方案2】:

    DictReader 是一个老式的对象,所以super() 在这里根本不起作用。您需要直接访问父类中的property 对象。在 Python 2 中,您要覆盖 .next() 方法,而不是 .__next__()

    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 csv.DictReader.fieldnames.fget(self)]
    
        def next(self):
            return DictInsensitive(csv.DictReader.next(self))
    

    演示:

    >>> example = '''\
    ... foo,Bar,BAZ
    ... 42,3.14159,Hello world!'''.splitlines()
    >>> csvDict = DictReaderInsensitive(example)
    >>> row = next(csvDict)
    >>> print row
    {'bar': '3.14159', 'foo': '42', 'baz': 'Hello world!'}
    >>> row['BAZ']
    'Hello world!'
    

    【讨论】:

    • 感谢两位的建议。我找到了解决这个问题的另一种方法,但老实说我不记得它是什么。我确实尝试了 Martijn,但它对我不起作用。
    • 很抱歉我的解决方案不适合您;如果你让我知道你遇到了什么问题,我也许可以帮助你克服这些问题。从我的回答中可以看出,我为您测试了代码。
    【解决方案3】:

    对于更简单的方法,您可以在访问字典之前简单地更新 DictReader.fieldnames 属性,如下所示:

    >>> f = open('example-x-y-time.csv', 'rb')
    >>> reader = csv.DictReader(f)
    >>> reader.fieldnames
    ['Latitude', 'Longitude', 'Date']
    >>> print next(reader)
    {'Latitude': '44.8982391', 'Date': '2004-07-12', 'Longitude': '-117.7791061'}
    >>> reader.fieldnames = [name.lower() for name in reader.fieldnames]
    >>> print next(reader)
    {'latitude': '44.6637001', 'date': '1964-04-03', 'longitude': '-123.5997009'}
    

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多