Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
如何跨越listA.txt和listB.txt的行,并返回一个output.txt,其中listB.txt的每一行在Python和grep中匹配?_Python_Regex - Fatal编程技术网

如何跨越listA.txt和listB.txt的行,并返回一个output.txt,其中listB.txt的每一行在Python和grep中匹配?

如何跨越listA.txt和listB.txt的行,并返回一个output.txt,其中listB.txt的每一行在Python和grep中匹配?,python,regex,Python,Regex,所以,我有两个邮件列表,我想交叉这些信息 “listA.txt”列表将用作检查“listB.txt”列表的基础,其中不仅有电子邮件,还有文本。然后将生成一个“output.txt”文件,其中“listA.txt”中列出的电子邮件将出现在“listB.txt”中的整行 例如: listA.txt arthur@gmail.com guinevere@outlook.com lancelot@yahoo.com gawaine@gmail.com listB.txt arthur@g

所以,我有两个邮件列表,我想交叉这些信息

“listA.txt”列表将用作检查“listB.txt”列表的基础,其中不仅有电子邮件,还有文本。然后将生成一个“output.txt”文件,其中“listA.txt”中列出的电子邮件将出现在“listB.txt”中的整行

例如:

listA.txt     

arthur@gmail.com
guinevere@outlook.com
lancelot@yahoo.com
gawaine@gmail.com

listB.txt

arthur@gmail.com Arthur Smith
guinevere@outlook.comtest
lance@gmail.com
gawaine@gmail.com Ocean Bvd
arth@hotmail.com

output.txt

arthur@gmail.com Arthur Smith
guinevere@outlook.comtest
gawaine@gmail.com Ocean Bvd
请注意,我需要将“listA.txt”的每一行作为一个模式来检查不仅包含“listA.txt”的所有整行的“listB.txt”行,而且还返回“output.txt”中显示这些电子邮件的“listB.txt”的所有整行。我不想要部分结果,因为在另一个算法中已经发生了这种情况,其中“lancelot@yahoo.com“已拆分(拆分行)并与匹配”lance@gmail.com“

我搜索了一个多星期,包括stackoverflow,但我没有找到任何解决方案,也不知道该怎么办,你能帮我吗?我试过grep、regex等,但也没有成功

我的尝试:

with open('emails4.txt', 'r', encoding="latin-1") as k: 
keywords = k.read().splitlines() 
results = [] 

with open('emails5.txt', encoding="latin-1") as f, open('output.txt', 'w', 
encoding="latin-1") as o: 
keywords2 = f.read().splitlines() 
for line in keywords2: 
if any(line in key for key in keywords):
o.write(line + "\n")
提前感谢。

A=[
"arthur@gmail.com",
"guinevere@outlook.com",
"lancelot@yahoo.com",
"gawaine@gmail.com",
]
B=[
"arthur@gmail.com亚瑟·史密斯“,
"guinevere@outlook.comtest",
"lance@gmail.com",
"gawaine@gmail.com海洋Bvd“,
"arth@hotmail.com",
]
#单行线
输出=[b代表b中的b,如果有的话(b.startswith(a)代表a中的a)]
打印(输出)
#“更长”的解决方案
输出=[]
对于b中的b:
对于a中的a:
如果b.从(a)开始:
输出追加(b)
打破
打印(输出)
复杂性是二次的-
len(A)*len(B)

编辑:带文件的解决方案:

打开(“listA.txt”)作为f:listA=f.read().splitlines()
将open(“listB.txt”)设置为f:listB=f.read().splitlines()
输出=[b代表列表中的b(如果有)(b.startswith(a)代表列表中的a)]
打开(“output.txt”、“w”)作为f:f.writelines(l+“\n”表示输出中的l)

“我尝试过…”-请提供尝试使用open('emails4.txt',r',encoding=“拉丁-1”)作为k:keywords=k.read().splitlines()结果=[]的
code
,将open('emails5.txt',encoding=“拉丁-1”)作为f,将open('output.txt',w',encoding=“拉丁-1”)作为o:keywords2=f.read().splitlines()对于line-in关键字2:如果有(line-in-key代表key-in关键字):o.write(line+“\n”)
code
我的意思是,下次直接将该信息放入问题中(您现在也可以编辑该问题),但我如何使用文件完成此操作?我应该如何处理这些文件?