【发布时间】:2017-01-14 08:45:17
【问题描述】:
我刚刚阅读了来自SlothRemove lines that contain certain string 问题的精彩回复,同时正在寻找一种过滤掉 txt / csv 文件中的垃圾行的方法。要点是“从输入文件中获取 x y z 单词/字符串/任何内容,然后过滤每一行,只写未过滤的行。”
他发布的代码是:
bad_words = ['bad', 'naughty']
with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
if not any(bad_word in line for bad_word in bad_words):
newfile.write(line)
我的问题是:有人能解释一下if not any(bad_word in line for bad_word in bad_words): 吗?
我尝试输入if not any(bad_word in line):,但它给了我一个错误。
我试图理解为什么。在 python 文档网页上粗略搜索对我没有帮助(我是 Python/编程的新手,可能不太聪明,无法启动 :-))。
感谢我阅读的任何参考资料。
谢谢!
【问题讨论】:
-
谢谢,我正在检查那个页面。你一定是一个快速阅读者,你几乎在我发布后立即回复。
标签: list python-2.7 if-statement syntax