在另一个 .txt 文件中逐位搜索一个 .txt 文件的内容段并打印匹配行
Search for content segments of one .txt file in another .txt file digit by digit and print matching lines
我有两个 txt 文件:file1.txt 和 file2.txt。
File1.txt 包含以下内容:
12345678
File2.txt 包含这个:
34567999
23499899
13571234
我现在想查看 file1.txt 第 1 行的前 3 位数字(即“123”)。我现在想转到 file2.txt 并搜索这三个数字(“123”)。当我在一行中按顺序找到这些数字时(即:第 3 行就是这种情况:13571234),我想将这一行写入一个新文件: file_new.txt.
然后,如果file2.txt中的所有行都从file1.txt(“123”)中搜索了这个序列,我想在file1.txt中再移动一位,这样新的搜索查询是“234”。现在,我想再次转到 file2.txt 以搜索所有带有“234”的序列,(即:第 2 行(23499899)和第 3 行(13571234))。由于第3行已经包含在file_new.txt中,我只想将第2行写到file_new.txt。
我想继续这个过程,搜索接下来的三个数字,直到 file1.txt 中的整行都在 file2.txt 中被搜索到。
有人可以帮我解决这个问题吗?
我在这里得到了一些解决方案:
f1 = open('file1.txt', 'r') # open in read mode
for digit in range(len(f1.readlines()[0])-2):
threedigits = f1.readlines()[0][digit:digit+3] # This is the first three digits
f2 = open('file2.txt', 'r') # open in read mode
lines = f2.readlines() # we read all lines
f2.close()
file_new = []
for i in lines:
if firstthreedigits in i:
file_new.append(i) # we add each lines containing the first three digits
f3 = open('file_new.txt', 'w') # open in write mode
for i in range(len(file_new)):
f3.write(file_new[i]) # we write all lines with first three digits
f3.close()
f1.close()
这个应该可以
您可以使用 readlines 将文本文件读入列表,然后使用 while 循环生成一个新的列表 L,如下所示。然后您可以将此列表 L 写入文本文件。
with open(file1_path) as file1:
search_string = file1.readlines()[0]
with open(file2_path) as file2:
strings_to_search = file2.readlines()
L= []
n=0
while n < len(search_string):
for i in strings_to_search:
if search_string[n:n+3] in i and i not in L:
L.append(i)
n +=1
我有两个 txt 文件:file1.txt 和 file2.txt。
File1.txt 包含以下内容:
12345678
File2.txt 包含这个:
34567999
23499899
13571234
我现在想查看 file1.txt 第 1 行的前 3 位数字(即“123”)。我现在想转到 file2.txt 并搜索这三个数字(“123”)。当我在一行中按顺序找到这些数字时(即:第 3 行就是这种情况:13571234),我想将这一行写入一个新文件: file_new.txt.
然后,如果file2.txt中的所有行都从file1.txt(“123”)中搜索了这个序列,我想在file1.txt中再移动一位,这样新的搜索查询是“234”。现在,我想再次转到 file2.txt 以搜索所有带有“234”的序列,(即:第 2 行(23499899)和第 3 行(13571234))。由于第3行已经包含在file_new.txt中,我只想将第2行写到file_new.txt。
我想继续这个过程,搜索接下来的三个数字,直到 file1.txt 中的整行都在 file2.txt 中被搜索到。
有人可以帮我解决这个问题吗?
我在这里得到了一些解决方案:
f1 = open('file1.txt', 'r') # open in read mode
for digit in range(len(f1.readlines()[0])-2):
threedigits = f1.readlines()[0][digit:digit+3] # This is the first three digits
f2 = open('file2.txt', 'r') # open in read mode
lines = f2.readlines() # we read all lines
f2.close()
file_new = []
for i in lines:
if firstthreedigits in i:
file_new.append(i) # we add each lines containing the first three digits
f3 = open('file_new.txt', 'w') # open in write mode
for i in range(len(file_new)):
f3.write(file_new[i]) # we write all lines with first three digits
f3.close()
f1.close()
这个应该可以
您可以使用 readlines 将文本文件读入列表,然后使用 while 循环生成一个新的列表 L,如下所示。然后您可以将此列表 L 写入文本文件。
with open(file1_path) as file1:
search_string = file1.readlines()[0]
with open(file2_path) as file2:
strings_to_search = file2.readlines()
L= []
n=0
while n < len(search_string):
for i in strings_to_search:
if search_string[n:n+3] in i and i not in L:
L.append(i)
n +=1