Python程序查找两个文本文件中的唯一行
很多时候我们会看到两个看起来很相似但又有一些区别的文件。如果文件很大或内容很多,手动搜索差异或查找文件中的唯一性并不容易。然而,使用Python程序可以轻松解决查找两个文本文件中唯一行的这个问题。本文通过三个不同的例子,给出了三种不同的方法来查找两个文本文件中唯一行的方法。使用的文本文件是a.txt和b.txt,最终结果存储在另一个txt文件中。
对于这些例子,txt文件中内容或行的差异如下所示:
| 文本文件中给出的行 | 在a.txt中 | 在b.txt中 |
|---|---|---|
计算机导论 |
是 |
是 |
编程概念导论 |
是 |
是 |
Windows导论,其特性和应用 |
是 |
是 |
C++编程 |
否 |
是 |
计算机组织原理 |
是 |
是 |
数据库管理系统 |
是 |
是 |
嵌入式系统导论 |
是 |
是 |
PHP基础 |
是 |
是 |
计算机科学的数学基础 |
是 |
否 |
Java编程 |
是 |
是 |
函数 |
是 |
是 |
数组 |
是 |
是 |
磁盘操作系统 |
是 |
是 |
数制和代码导论 |
否 |
是 |
数据挖掘 |
是 |
是 |
软件工程 |
是 |
否 |
计算机网络 |
是 |
是 |
控制结构 |
是 |
是 |
示例1 - 通过迭代和比较两个文件中各个行来查找两个文本文件中的唯一行。
算法
步骤1 - 以读取模式打开两个文本文件。
步骤2 - 读取a.txt中的行到afile,读取b.txt中的行并将其存储到bfile。
步骤3 - 创建一个名为cfile的空列表。逐行遍历bfile。如果一行不在afile中,则将其添加到cfile。
步骤4 - 现在逐行遍历afile。如果一行不在bfile中,则将其添加到cfile。将cfile写入finalRes.txt。
步骤5 - 运行程序,然后检查结果。
Python文件包含这些内容
af = open('a.txt', 'r')
afile = af.readlines()
bf = open('b.txt', 'r')
bfile = bf.readlines()
cfile=[]
for ln in bfile:
if ln not in afile:
cfile.append(ln)
for ln in afile:
if ln not in bfile:
cfile.append(ln)
resultFile= open('finalRes.txt', 'w')
for lin in cfile:
resultFile.write(lin)
查看结果 - 示例1
要在cmd窗口中查看两个txt文件中的唯一行作为结果,请运行Python文件。
C++ Programming Mathematical Foundation For Computer Science Software Engineering
图1:名为finalRes.txt的结果文件的内容。
示例2:使用difflib库模块查找两个文本文件中的唯一行。
算法
步骤1 - 首先从difflib导入Differ模块。
步骤2 - 以读取模式打开两个文本文件。
步骤3 - 读取a.txt中的行到afile,读取b.txt中的行并将其存储到bfile。
步骤4 - 使用Differ模块比较文件差异。将结果写入finalRes1.txt。
步骤5 - 运行程序,然后检查结果。
Python文件包含这些内容
from difflib import Differ
af = open('a.txt', 'r')
afile = af.readlines()
bf = open('b.txt', 'r')
bfile = bf.readlines()
result = list(Differ().compare(afile, bfile))
resultFile= open('finalRes1.txt', 'w')
for lin in result:
resultFile.write(lin)
查看结果 - 示例2
打开cmd窗口并运行python文件以查看结果。结果文件将在两个文件的唯一行前显示“-”或“+”。“+”号表示该行未在第一个txt文件中给出,而“-”号表示该行未出现在第二个txt文件中。
Introduction to Computers Introduction to Programming Concepts Introduction to Windows, its Features, Application + C++ Programming Computer Organization Principles Database Management Systems Introduction to Embedded Systems Fundamentals of PHP - Mathematical Foundation For Computer Science Java Programming Functions Arrays Disk Operating System Introduction to Number system and codes Data Mining - Software Engineering Computer Networks Control Structures
图2:名为finalRes1.txt的结果文件的内容
示例3:通过删除相似行并保留唯一行来查找两个文本文件中的唯一行。
算法
步骤1 - 以读取模式打开两个文本文件。
步骤2 - 读取a.txt中的行到afile,并打开b.txt并将其存储到bf。
步骤3 - 对于bf中的所有行,如果该行在afile中,则将其从afile中删除。如果它不在afile中,则将其添加到另一个名为uniqueB的列表中。
步骤4 - 将afile中剩下的行和uniqueB中的行添加到cfile。将cfile写入finalRes2.txt。
步骤5 - 部署程序,然后检查结果。
Python文件包含这些内容
with open('a.txt', 'r') as af:
afile = set(af)
uniqueB = []
cfile=[]
with open('b.txt', 'r') as bf:
for ln in bf:
if ln in afile:
afile.remove(ln)
else:
uniqueB.append(ln)
print("\nPrinting all unique lines in both a.txt and b.txt : ")
print('\nAll the lines in a.txt file that are not in b.txt: \n')
for ln in sorted(afile):
print(ln.rstrip())
cfile.append(ln)
print()
print('\nAll the lines in b.txt file that are not in a.txt: \n')
for lin in uniqueB:
print(lin.rstrip())
cfile.append(lin)
print()
resultFile= open('finalRes2.txt', 'w')
for lin in cfile:
resultFile.write(lin)
查看结果 - 示例3
要在cmd窗口中查看两个txt文件中的唯一行作为结果,请运行Python文件。
Mathematical Foundation For Computer Science Software Engineering C++ Programming
图3:名为finalRes2.txt的结果文件的内容。
结论
在这篇Python文章中,通过三个不同的例子,介绍了如何查找两个文本文件中唯一行的方法。在示例1中,通过逐行遍历两个txt文件来使用简单的迭代和比较。在示例2中,使用了difflib中的Differ库模块。在示例3中,使用Python列表删除相似行,同时保留唯一行。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP