Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python打印一行,当且仅当找到x和y时_Python - Fatal编程技术网

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

python打印一行,当且仅当找到x和y时,python,Python,我试图打印一行,其中包含特定的关键字,它们应该在同一行中,以便打印出来 这就是我解决问题的方法,我制作了两个变量,并给它们分配了变量。其中一个是文件名,每次程序遍历列表时,文件名都会更改 for files in myfiles: for lines in info: if dir_key and files in lines: print lines else:

我试图打印一行,其中包含特定的关键字,它们应该在同一行中,以便打印出来

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

for files in myfiles:
       for lines in info:
             if dir_key and files in lines:
                      print lines
             else:
                 print "line not found" 
这是可行的,但问题是,它还打印一行,其中只包含文件名。我如何确保它不会那样做?。我试过了

if line.beginswith(dir_key) and line.endswith(file)
但这并没有产生任何结果。所以我改用第一种方法。

也许:

if dir_key in lines and files in lines:

如果dir_key是一个字符串,那么只要dir_key不是空字符串,测试if dir_key:就会为真。因此,您的总体测试很可能相当于if True和files in line:

您现在所做的是检查of dir_键,而不是检查dir_键是否在line中。改变

if dir_key and files in lines:
#to
if dir_key in lines and files in lines: