在Python中逐行比较两个文件
在Python中,有许多方法可用于这种比较。在本文中,我们将了解如何 逐行比较两个不同的文件。 Python支持许多模块来这样做,在这里我们将讨论使用其各种模块的方法。
本文使用两个示例文件进行实现。
使用中的文件:
- 文件.txt
- 文件1.txt
方法一:使用unified_diff()
Python有一个模块,专门用于比较文件之间的差异。要使用difflib库获取差异,我们必须调用统一的_diff()函数进行此比较。
Syntax:
unified_diff(file1, file2, fromfile, tofile, lineterm)
Parameter:
- file1: List of String such as file_1_text
- file2: List of String such as file_2_text
- fromfile: first file name with extension
- tofile: second file name with extension
- lineterm: argument to “” so that the output will be automcally uniformly newline free
方法
- 导入模块
- 打开文件
- 使用 Unified_diff() 与适当的属性进行比较
例子:
Python3
# Importing difflib
import difflib
with open('file1.txt') as file_1:
file_1_text = file_1.readlines()
with open('file2.txt') as file_2:
file_2_text = file_2.readlines()
# Find and print the diff:
for line in difflib.unified_diff(
file_1_text, file_2_text, fromfile='file1.txt',
tofile='file2.txt', lineterm=''):
print(line)
Python3
from difflib import Differ
with open('file1.txt') as file_1, open('file2.txt') as file_2:
differ = Differ()
for line in differ.compare(file_1.readlines(), file_2.readlines()):
print(line)
Python3
# Open File in Read Mode
file_1 = open('file1.txt', 'r')
file_2 = open('file2.txt', 'r')
print("Comparing files ", " @ " + 'file1.txt', " # " + 'file2.txt', sep='\n')
file_1_line = file_1.readline()
file_2_line = file_2.readline()
# Use as a COunter
line_no = 1
print()
with open('file1.txt') as file1:
with open('file2.txt') as file2:
same = set(file1).intersection(file2)
print("Common Lines in Both Files")
for line in same:
print(line, end='')
print('\n')
print("Difference Lines in Both Files")
while file_1_line != '' or file_2_line != '':
# Removing whitespaces
file_1_line = file_1_line.rstrip()
file_2_line = file_2_line.rstrip()
# Compare the lines from both file
if file_1_line != file_2_line:
# otherwise output the line on file1 and use @ sign
if file_1_line == '':
print("@", "Line-%d" % line_no, file_1_line)
else:
print("@-", "Line-%d" % line_no, file_1_line)
# otherwise output the line on file2 and use # sign
if file_2_line == '':
print("#", "Line-%d" % line_no, file_2_line)
else:
print("#+", "Line-%d" % line_no, file_2_line)
# Print a empty line
print()
# Read the next line from the file
file_1_line = file_1.readline()
file_2_line = file_2.readline()
line_no += 1
file_1.close()
file_2.close()
输出:
— file1.txt
+++ file2.txt
@@ -1,5 +1,5 @@
Learning
Python
is
-too
-simple.
+so
+easy.
方法 2:使用不同
有一个类可用于比较difflib库中名为Differ的文件之间的差异。此类用于比较文本行的序列,并生成人类可读的差异或增量。 ‘-‘ ‘+’ ‘ ‘ ‘?’Code Meaning line unique to sequence 1 line unique to sequence 2 line common to both sequences line not present in either input sequence
方法
- 导入模块
- 打开文件
- 读取内容行 bt 行
- 使用不同的类对象调用比较函数
例子:
蟒蛇3
from difflib import Differ
with open('file1.txt') as file_1, open('file2.txt') as file_2:
differ = Differ()
for line in differ.compare(file_1.readlines(), file_2.readlines()):
print(line)
输出:
Learning
Python
is
– too
– simple.
+ so
+ easy.
方法三:使用while循环和Intersection方法
方法
- 以读取模式打开两个文件
- 存储字符串列表
- 在常见字符串的intersection() 方法的帮助下开始比较两个文件
- 使用 while 循环比较两个文件的差异
- 关闭两个文件
例子:
蟒蛇3
# Open File in Read Mode
file_1 = open('file1.txt', 'r')
file_2 = open('file2.txt', 'r')
print("Comparing files ", " @ " + 'file1.txt', " # " + 'file2.txt', sep='\n')
file_1_line = file_1.readline()
file_2_line = file_2.readline()
# Use as a COunter
line_no = 1
print()
with open('file1.txt') as file1:
with open('file2.txt') as file2:
same = set(file1).intersection(file2)
print("Common Lines in Both Files")
for line in same:
print(line, end='')
print('\n')
print("Difference Lines in Both Files")
while file_1_line != '' or file_2_line != '':
# Removing whitespaces
file_1_line = file_1_line.rstrip()
file_2_line = file_2_line.rstrip()
# Compare the lines from both file
if file_1_line != file_2_line:
# otherwise output the line on file1 and use @ sign
if file_1_line == '':
print("@", "Line-%d" % line_no, file_1_line)
else:
print("@-", "Line-%d" % line_no, file_1_line)
# otherwise output the line on file2 and use # sign
if file_2_line == '':
print("#", "Line-%d" % line_no, file_2_line)
else:
print("#+", "Line-%d" % line_no, file_2_line)
# Print a empty line
print()
# Read the next line from the file
file_1_line = file_1.readline()
file_2_line = file_2.readline()
line_no += 1
file_1.close()
file_2.close()
输出:
Comparing files
@ file1.txt
# file2.txt
Common Lines in Both Files
Learning
Python
is
Difference Lines in Both Files
@- Line-4 too
#+ Line-4 so
@- Line-5 simple.
#+ Line-5 easy.