Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 我想搜索txt文件中的列表元素_Python_List_Search_Element_Readline - Fatal编程技术网

Python 我想搜索txt文件中的列表元素

Python 我想搜索txt文件中的列表元素,python,list,search,element,readline,Python,List,Search,Element,Readline,我想在txt文件中搜索列表元素。然而,使用这段代码,我没有得到期望的结果。我试过几种解决办法,但都不管用 def运行(自): hostname=self.queue.get() 将open('allhosts.txt',“r+”)作为文件1: fileline1=file1.readlines() 对于主机名中的x: 对于fileline1中的行: 如果x在直线上: self.found.emit(主机名) 其他: self.notFound.emit(主机名) 在这种情况下使用regx imp

我想在txt文件中搜索列表元素。然而,使用这段代码,我没有得到期望的结果。我试过几种解决办法,但都不管用

def运行(自):
hostname=self.queue.get()
将open('allhosts.txt',“r+”)作为文件1:
fileline1=file1.readlines()
对于主机名中的x:
对于fileline1中的行:
如果x在直线上:
self.found.emit(主机名)
其他:
self.notFound.emit(主机名)
在这种情况下使用regx

import re

str = open('a.txt', 'r').read()

m = re.search('(?<=hostname)(.*)', str)
print ("hostname",(m.groups()))
重新导入
str=open('a.txt','r')。read()

m=re.search(“(?您得到了什么结果,您希望得到什么?使用此代码,它还返回
if
else
语句。因此它正在查找它们,而没有找到它们。这不可能是真的。如果执行
if
分支,则不会执行
else
分支,反之亦然(至少不在同一迭代中)。请显示a。您希望
x
是什么?