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 在文件中查找列表中的项目,并从匹配行中提取列_Python_List_File_Pattern Matching - Fatal编程技术网

Python 在文件中查找列表中的项目,并从匹配行中提取列

Python 在文件中查找列表中的项目,并从匹配行中提取列,python,list,file,pattern-matching,Python,List,File,Pattern Matching,我的名单如下: [abc,efg,dfg] [tac,thc,kfl] [etc,acy,qof] 我想拉出一些行,这些行包含该行列表中所有的值,例如如果我有一个文件 EAF 123 abc 567 efg dfg TAC 234 tic 576 qud tic QAC 586 qwa 786 tov ypv CVB 078 abc efg dfg qtc TCB 398 tac thc kfl tqo OCN 068 etc acy qof qic 我得到包含列表组件的行

我的名单如下:

 [abc,efg,dfg]
 [tac,thc,kfl]
 [etc,acy,qof]
我想拉出一些行,这些行包含该行列表中所有的值,例如如果我有一个文件

 EAF 123 abc 567 efg dfg
 TAC 234 tic 576 qud tic
 QAC 586 qwa 786 tov ypv
 CVB 078 abc efg dfg qtc
 TCB 398 tac thc kfl tqo
 OCN 068 etc acy qof qic
我得到包含列表组件的行,如下所示:

 123 abc 567 efg dfg     
 398 tac thc kfl tqo
 068 etc acy qof qic
我正在尝试使用以下代码:

 textfile = open("file_checkin.txt",'rb')
  for lines in textfile.xreadlines():
   if any(value in lines for value in list):
    print lines.strip()
   else:
    continue

但是,是否有一个选项,我可以使用它来替换列表中与该行匹配的所有项目

使用
all
语句。另一个:您真的需要使用
吗?其他:继续
?您不能省略它吗?这是较短的代码:
pprint([lines.strip()用于textfile.xreadlines()中的行(line中的值用于list中的值))
。您需要从pprint导入pprint
。您好@Elmex80s是的,我可以省略其他部分:我尝试过实现它,它确实有效,谢谢