eng*_*vix 2 python regex search file
我的问题与此类似,只是我想搜索多个出现的情况chars,例如g,d和e,然后打印其中存在所有指定字符的行。
我已尝试以下方法,但没有成功:
searchfile = open("myFile.txt", "r")
for line in searchfile:
if ('g' and 'd') in line: print line,
searchfile.close()
Run Code Online (Sandbox Code Playgroud)
我得到的行中包含“g”或“d”或两者,我想要的只是两者都出现,而不是至少其中之一,就像运行上述代码的结果一样。
if set('gd').issubset(line)
Run Code Online (Sandbox Code Playgroud)
这样做的优点是每次检查都不会遍历整c in line行,因此不会经历两次
| 归档时间: |
|
| 查看次数: |
15647 次 |
| 最近记录: |