如何在没有正则表达式的情况下不区分大小写地搜索字符串?

时间:2012-08-10 13:08:08

标签: python

我想在文件中搜索包含给定字符串但没有匹配大小写的所有行。如何使这段代码不区分大小写呢?

with open(logfile) as inf:
    for line in inf:
        if var in line:
            print 'found',line

1 个答案:

答案 0 :(得分:6)

with open(logfile) as fin:
    for line in fin:
        if var.lower() in line.lower():  # makes this case insensitive
            print 'found', line.rstrip() # You will be printing double new lines 
                                         #  without rstrip here