How to search for a string case-insensitively without regular expressions?

前端 未结 1 1073
逝去的感伤
逝去的感伤 2021-01-25 16:31

I want to search a file for all lines containing a given string without matching case. How can I make this code case-insensitive?



        
相关标签:
1条回答
  • 2021-01-25 17:03
    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
    
    0 讨论(0)
提交回复
热议问题