简体   繁体   English

访问csv标头空白区域并且不区分大小写

[英]Accessing csv header white space and case insensitive

I'm overriding the csv.Dictreader.fieldnames property like the following to read all headers from csv files without white space and in lower case. 我正在覆盖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]

Now my question is, how can I access the fieldnames with automatically strip() and lower() the query? 现在我的问题是,如何使用自动strip()lower()查询来访问字段名?

This is, how I do it manually: 这是我如何手动完成的:

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

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

Any ideas? 有任何想法吗?

Based on Pedro Romano's suggestion I coded the following example. 根据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())

For a file containing headers like 对于包含标题的文件

  • "column_A" “column_A”
  • " column_A" “column_A”
  • "Column_A" “Column_A”
  • " Column_A" “Column_A”
  • ... ...

you can access the columns like this: 你可以像这样访问列:

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

You'll have to do it in two steps: 你必须分两步完成:

  1. Create your dict specialisation with a __getitem__ method that applies the .strip().lower() to the its key parameter. 使用__getitem__方法创建dict ,该方法将.strip().lower()应用于其key参数。
  2. Override __next__ on your MyDictReader specialised class to return one of your special dictionaries initialised with the dictionary returned by the csv.DictReader superclass's __next__ method. 覆盖__next__MyDictReader专门的类来回报您与返回的字典初始化特殊的一个词典csv.DictReader超类的__next__方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM