在python中的另一个文件行中查找模式

在python中的另一个文件行中查找模式,python,database,loops,file,pattern-matching,Python,Database,Loops,File,Pattern Matching,我正在学习python,因此我有两个包含大量行的文件: 文件1 71528 1452 14587 文件2 country_hoj_17458 9 CA 5 CA 78.4 country_hoj_1452 10 CA 15 CA 96.5 country_hoj_14787 19 CA 51 CA 12.4 country_hoj_15742 19 CA 51 CA 12.4 country_hoj_171528 19 CA 51

我正在学习python,因此我有两个包含大量行的文件:

文件1

71528
1452
14587
文件2

country_hoj_17458   9  CA   5    CA 78.4
country_hoj_1452   10  CA   15   CA 96.5
country_hoj_14787  19  CA   51   CA 12.4
country_hoj_15742  19  CA   51   CA 12.4
country_hoj_171528  19  CA   51   CA 12.4
我想打印第一列中模式(编号)文件1与文件2匹配的行。我想要一个像这样的文件

 country_hoj_1452   10  CA   15   CA 96.5
 country_hoj_14787  19  CA   51   CA 12.4
我的剧本是这样的:

filename = numbers.txt
filename2 = data.txt
with open(filename) as f:
    with open (filename2) as m:
        for line in f:
                if line in m:
                       print (m)
     
我需要在里面修好什么?谁能帮助我,支持我?非常感谢

filename = 'numbers.txt'
filename2 = 'data.txt'

with open(filename) as numberLines:
    with open (filename2) as dataLines:
        nL = numberLines.read().splitlines()
        dL = dataLines.read().splitlines()
        dataReadLines = [j for i in nL for j in dL if i in j]
        #dataReadLines = [i for i in nL]
        print (str(dataReadLines))
另一个答案是每个键与其各自的数据配对。我已经更改了您的输入,您可以使用下面的代码轻松理解

from collections import defaultdict

filename = 'numbers.txt'
filename2 = 'data.txt'

with open(filename) as numberLines:
    with open (filename2) as dataLines:
        nL = numberLines.read().splitlines()
        dL = dataLines.read().splitlines()
        defDList = defaultdict(list)
        dataReadLines = [defDList[i].append(j) for i in nL for j in dL if i in j]
        #dataReadLines = [i for i in nL]
        print (defDList)

您需要重复有关字符串处理的教程材料。学习使用运算符中的
。堆栈溢出不是为了替换现有教程材质。