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