我需要在这个抓取的本地driver list里面查找我需要的driver的版本,用来检查本地的driver是否为我需要的版本。创建两个文本一个装的是本地的driver list,暂命名为1。然后是我的需求driver文本名字为2。同时我需求的driver文本里面的driver版本和数量不止一个。然后在1里面查找2的内容。并将2在1中未找到的输出为新的文本3。通过python实现
# 打开文件 1 并读取其中的所有行
with open('1.txt', 'r') as file1:
lines1 = file1.readlines()
# 打开文件 2 并读取其中的所有行
with open('2.txt', 'r') as file2:
lines2 = file2.readlines()
# 创建一个空列表,用于存储未找到的内容
not_found = []
# 循环遍历文件 2 中的每一行
for line in lines2:
# 如果当前行不在文件 1 中,则将其添加到 not_found 列表中
if line not in lines1:
not_found.append(line)
# 如果有未找到的内容,则将其写入新的文件 3
if not_found:
with open('3.txt', 'w') as file3:
file3.writelines(not_found)
您可以使用 Python 的内置函数 open() 和 readlines() 来读取文本文件 1 和文件 2 的内容。然后,您可以使用一个循环来遍历文件 2 中的行,并检查这些行是否在文件 1 中出现。如果某行在文件 1 中未找到,则将其记录在新的列表中。最后,您可以使用 open() 函数和 writelines() 函数将列表写入文件 3。
以下是一个示例代码,它实现了上述过程:
# 读取文件 1 和文件 2 的内容
with open('1.txt', 'r') as f1:
lines1 = f1.readlines()
with open('2.txt', 'r') as f2:
lines2 = f2.readlines()
# 创建一个空列表,用于存储未找到的行
not_found = []
# 遍历文件 2 中的行
for line in lines2:
# 如果行未在文件 1 中找到,将其记录在列表中
if line not in lines1:
not_found.append(line)
# 将未找到的行写入文件 3
with open('3.txt', 'w') as f3:
f3.writelines(not_found)
实现代码如下:
# 文件1用1.txt表示,文件2用2.txt表示,文件3用3.txt表示
file1 = open('1.txt', 'r')
with open('2.txt', 'r') as file2:
for line2 in file2:
if line2 in file1:
print('文件2中的此项内容可以在文件1中找到!')
else:
file3 = open('3.txt', 'a+')
file3.write(line2)
file3.close()
file1.close()