我有2个txt文件(a和b)。在
文件_a.txt包含一个4个字母组合的长列表(每行一个组合):
aaaa
bcsg
aacd
gdee
aadw
hwer
etc.
文件_b.txt包含不同长度的字母组合列表(有些带有空格):
^{pr2}$我正在寻找一个允许我执行以下操作的python脚本:
文件aaai.txt文件公司名称:
aaaibjkes
aaailoiersaaageehikjaaa
aaailoiuwegoiglkjaaake
etc.
文件密件索引.txt公司名称:
bcspwiopiejowih
bcsiweyoieotpwe
etc.
对不起,我是个新手。有人能给我指一下正确的方向吗。到目前为止,我只有:
#I presume I will have to use regex at some point
import re
file1 = open('file_a.txt', 'r').readlines()
file2 = open('file_b.txt', 'r').readlines()
#Should I look into findall()?
我希望这对你有帮助
试试这个:
您可以做的是打开两个文件并使用
for
循环逐行运行这两个文件。在您可以有两个
for
循环,第一个循环读取file_a.txt
,因为您将只读取一次。第二个将通读file_b.txt
,并在开始处查找字符串。在为此,您必须使用
.find()
来搜索字符串。因为它在开始处,所以值应该是0
。在测试用例:
我在您的问题中使用测试用例:
文件_a.txt
^{pr2}$文件
该程序生成一个输出文件
bcsg.txt
,正如它所期望的那样,其中包含bcsgiweyoieotpwe
。在相关问题 更多 >
编程相关推荐