python - 在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)
解决方案
使用upper
:
if not any(bad_word.upper() in line.upper() for bad_word in bad_words):
upper
如果它们中的任何一个是小写,这将使用两次。
推荐阅读
- r - 如何让散点图为我正在绘制的每个值具有不同的颜色?
- php - API 响应错误:无法反序列化实例
- java - 图形对象返回空指针
- javascript - 选择其中一个复选框后如何使按钮处于活动状态?以及如何将值从复选框动态传输到 rezBlock_1 块?
- python - 使用 BeautifulSoup 和 python 抓取 Instagram Hashtag 页面
- android - android构建配置中的unity 2019错误
- python - 发电集算法实现
- docker - docker-compose 不会启动超过大约。文件中定义的 130 个服务容器 50 个
- windows - 使用 powershell 脚本将 RSAT 安装到具有提升权限的 Windows 10
- sql - 仅查询每个用户的最新条目