python中不区分大小写的CSV查询

时间:2018-09-21 21:30:07

标签: python excel csv

我有在弗吉尼亚州道路上发生动物碰撞的数据集。我正在尝试提取涉及熊的所有碰撞。在我的数据集中,使用至少三种不同的大小写组合(即Bear,bear,BEAR)输入bear。以下是我用来提取熊碰撞的代码:

import csv

with open('google.csv', 'r') as f:
    reader = csv.reader(f)
    lines = [ row for row in reader if "bear" in row[4] ]

with open('outfile.csv', "w") as outfile:
    writer = csv.writer(outfile)
    writer.writerows(lines)

如何使该名称不区分大小写?

1 个答案:

答案 0 :(得分:0)

尝试一下:

import csv

with open('google.csv', 'r') as f:
    reader = csv.reader(f)
    lines = [ row for row in reader if "bear" in row[4].strip().lower() ]

with open('outfile.csv', "w") as outfile:
    writer = csv.writer(outfile)
    writer.writerows(lines)