访问csv头空白和不区分大小写 [英] Accessing csv header white space and case insensitive
本文介绍了访问csv头空白和不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我覆盖 csv.Dictreader.fieldnames
属性,如下所示从csv文件读取所有标题,没有空格和小写。
import csv
class MyDictReader(csv.DictReader):
@property
def fieldnames self):
return [field.strip()。lower()for super(MyDictReader,self).fieldnames]
现在我的问题是,如何自动访问 strip()
和 lower code>查询?
这是我手动进行的操作:
csvDict = MyDictReader(open('csv-file.csv','rU'))
for csvDict中的lineDict:
query ='Column_A'.strip()。lower()
print(lineDict [query])
任何想法?
根据Pedro Romano的建议,我编写了以下示例。 import csv
类DictReaderInsensitive(csv.DictReader):
#此类覆盖csv.fieldnames属性。
#所有字段名都没有空格,小写字母
@property
def fieldnames(self):
return [field.strip对于字段在super(DictReaderInsensitive,self).fieldnames]
def __next __(self):
#从原来的__next__获取结果,但存储在DictInsensitive
dInsensitive = DictInsensitive()
dOriginal = super(DictReaderInsensitive,self).__ next __()
#存储来自旧的dict的所有对,在新的自定义的
key,value in dOriginal.items():
dInsensitive [key] = value
return dInsensitive
class DictInsensitive(dict):
#类覆盖__getitem__方法以自动剥离()和降低输入键
def __getitem __(self,key):
return dict .__ getitem __(self,key.strip lower()
对于包含
标题的文件$ b b
- column_A
- column_A
- Column_A
- Column_A
- ...
:
csvDict = DictReaderInsensitive(open('csv-file.csv','rU'))
for lineDict in csvDict:
print(lineDict ['Column_A'])#或
print(lineDict ['Column_A'])#或
print(lineDict ['column_a']) #all都返回相同的
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.
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?
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.
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"
- ...
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
这篇关于访问csv头空白和不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文