python打印一行,当且仅当找到x和y时

cyb*_*mon 0 python

我正在尝试打印包含特定关键字的行,并且它们都应该在同一行中以便打印出来.

这就是我解决问题的方法,我制作了2个变量,并为它们分配了变量.其中一个是文件名,每次程序迭代列表时都会更改.

for files in myfiles:
       for lines in info:
             if dir_key and files in lines:
                      print lines
             else:
                 print "line not found" 
Run Code Online (Sandbox Code Playgroud)

这是有效的,但问题是,它还打印一行只包含文件名.我如何确保不这样做?我试过了

if line.beginswith(dir_key) and line.endswith(file)
Run Code Online (Sandbox Code Playgroud)

但这并没有产生任何结果.所以我切换到第一种方法.

mgi*_*son 5

也许:

if dir_key in lines and files in lines:
Run Code Online (Sandbox Code Playgroud)

如果dir_key是一个字符串,则测试if dir_key:将是True只要dir_key是不是空字符串.所以你的总测试很可能相当于if True and (files in lines):