Python 在两个文本文件中查找唯一行程序

Python 在两个文本文件中查找唯一行程序

很多时候,我们会看到两个看起来相似但有细微差异的文件。如果文件很大或包含大量内容,手动搜索差异或找到文件中的唯一性并不容易。然而,使用Python程序可以轻松解决在两个文本文件中查找唯一行的问题。本文介绍了三个不同的示例,分别使用了三种不同的方法来查找两个文本文件中的唯一行。所使用的文本文件为a.txt和b.txt,最终结果存储在另一个txt文件中。

对于这些示例,txt文件中的内容或行差异如下所示:

文本文件中的行 在a.txt中 **在b.txt中 th>
计算机介绍
编程概念介绍
Windows及其特点和应用介绍
C++编程
计算机组织原理
数据库管理系统
嵌入式系统介绍
PHP基础
计算机科学的数学基础
Java编程
函数
数组
磁盘操作系统
数字系统和编码介绍
数据挖掘
软件工程
计算机网络
控制结构

示例1 – 通过迭代和比较两个文本文件中的各行来找到唯一行。

步骤

步骤1 - 以读取模式打开两个文本文件。

步骤2 - 在a.txt中逐行读取,在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)

结果

要查看两个文本文件中的唯一行作为结果,请在 cmd 窗口中运行 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)

结果

打开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中读取行,并打开b.txt,并将其存储在bf中。

步骤3 - 对于bf中的所有行,如果该行在a文件中,则从a文件中删除。如果不在a文件中,则将其追加到另一个名为uniqueB的列表中。

步骤4 - 将剩余的a文件中的行和uniqueB中的行追加到c文件中。将c文件写入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)

结果

为了查看作为结果的两个txt文件中的唯一行,运行Python文件在cmd窗口中。

Mathematical Foundation For Computer Science
Software Engineering
C++ Programming

图3:名为finalRes2.txt的结果文件的内容。

结论

在这篇Python文章中,使用三个不同的示例,介绍了如何在两个文本文件中找到唯一行的方法。在示例1中,通过逐行比较,在两个txt文件中逐行进行简单的迭代和比较。在示例2中,使用了一个名为Differ的库模块,来自difflib。在示例3中,使用Python列表删除相似的行,同时保留唯一的行。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程