首页 > 解决方案 > 在python中,如何使此代码不区分大小写

问题描述

此代码删除包含特定单词(坏词)的行 问题区分大小写

例如:它将删除“打印枕头”,但不会删除“打印枕头”或“打印枕头”

任何简单的修复方法(不区分大小写)

问候

bad_words = ['printed pillow', 'decalac', 'pandora']

with open('oldfile.csv') as oldfile, open('newfile.csv', 'w') as newfile:
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):
            newfile.write(line)

标签: python

解决方案


使用upper

if not any(bad_word.upper() in line.upper() for bad_word in bad_words):

upper如果它们中的任何一个是小写,这将使用两次。


推荐阅读