使用 Python 中的 difflib 比较两个 .txt 文件
- 2025-04-10 09:46:00
- admin 原创
- 20
问题描述:
我正在尝试比较两个文本文件并输出比较文件中不匹配的第一个字符串,但由于我对 Python 还很陌生,所以遇到了困难。有人能给我一个使用此模块的示例方法吗?
当我尝试类似的事情时:
result = difflib.SequenceMatcher(None, testFile, comparisonFile)
我收到一条错误消息,指出“文件”类型的对象没有 len。
解决方案 1:
首先,您需要将字符串传递给 difflib.SequenceMatcher,而不是文件:
# Like so
difflib.SequenceMatcher(None, str1, str2)
# Or just read the files in
difflib.SequenceMatcher(None, file1.read(), file2.read())
这将修复你的错误。
要获取第一个不匹配的字符串,请参阅difflib 文档。
解决方案 2:
这是一个使用 Python difflib 比较两个文件内容的简单示例...
import difflib
file1 = "myFile1.txt"
file2 = "myFile2.txt"
diff = difflib.ndiff(open(file1).readlines(),open(file2).readlines())
print ''.join(diff),
解决方案 3:
您确定这两个文件都存在吗?
刚刚测试了一下并得到了完美的结果。
为了获得结果我使用类似如下的方法:
import difflib
diff=difflib.ndiff(open(testFile).readlines(), open(comparisonFile).readlines())
try:
while 1:
print diff.next(),
except:
pass
每行第一个字符表示它们是否不同:例如:“+”表示添加了下一行,等等。
解决方案 4:
听起来你可能根本不需要 difflib。如果你逐行比较,请尝试以下操作:
test_lines = open("test.txt").readlines()
correct_lines = open("correct.txt").readlines()
for test, correct in zip(test_lines, correct_lines):
if test != correct:
print "Oh no! Expected %r; got %r." % (correct, test)
break
else:
len_diff = len(test_lines) - len(correct_lines)
if len_diff > 0:
print "Test file had too much data."
elif len_diff < 0:
print "Test file had too little data."
else:
print "Everything was correct!"
解决方案 5:
另一种更简单的方法是逐行检查两个文本文件是否相同。试试看。
fname1 = 'text1.txt'
fname2 = 'text2.txt'
f1 = open(fname1)
f2 = open(fname2)
lines1 = f1.readlines()
lines2 = f2.readlines()
i = 0
f1.seek(0)
f2.seek(0)
for line1 in f1:
if lines1[i] != lines2[i]:
print(lines1[i])
exit(0)
i = i+1
print("both are equal")
f1.close()
f2.close()
否则,python 中的 filecmp 中有一个预定义文件可供您使用。
import filecmp
fname1 = 'text1.txt'
fname2 = 'text2.txt'
print(filecmp.cmp(fname1, fname2))
:)
解决方案 6:
# -*- coding: utf-8 -*-
"""
"""
def compare_lines_in_files(file1_path, file2_path):
try:
with open(file1_path, 'r', encoding='utf-8') as file1, open(file2_path, 'r', encoding='utf-8') as file2:
lines_file1 = file1.readlines()
lines_file2 = file2.readlines()
mismatched_lines = []
# Compare each line in file1 to all lines in file2
for line_num, line1 in enumerate(lines_file1, start=1):
line1 = line1.strip() # Remove leading/trailing whitespace
found_match = False
for line_num2, line2 in enumerate(lines_file2, start=1):
line2 = line2.strip() # Remove leading/trailing whitespace
# Perform a case-insensitive comparison
if line1.lower() == line2.lower():
found_match = True
break
if not found_match:
mismatched_lines.append(f"Line {line_num} in File 1: '{line1}' has no match in File 2")
# Compare each line in file2 to all lines in file1 (vice versa)
for line_num2, line2 in enumerate(lines_file2, start=1):
line2 = line2.strip() # Remove leading/trailing whitespace
found_match = False
for line_num, line1 in enumerate(lines_file1, start=1):
line1 = line1.strip() # Remove leading/trailing whitespace
# Perform a case-insensitive comparison
if line2.lower() == line1.lower():
found_match = True
break
if not found_match:
mismatched_lines.append(f"Line {line_num2} in File 2: '{line2}' has no match in File 1")
return mismatched_lines
except FileNotFoundError:
print("One or both files not found.")
return []
# Paths to the two text files you want to compare
file1_path = r'C:Python SpaceT1.txt'
file2_path = r'C:Python SpaceT2.txt'
mismatched_lines = compare_lines_in_files(file1_path, file2_path)
if mismatched_lines:
print("Differences between the files:")
for line in mismatched_lines:
print(line)
else:
print("No differences found between the files.")
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD