小问题求助
需求是想做个比较两个文档的内容有什么区别,主要是一行一行比较,然后看到了difflib 这个功能,感觉蛮符合我的要求,我目前写出来的是能比较,但是是一个一个字比较的,怎么变成一行行比较
import difflib
text1 = input("请输入文档1路径:")
text2 = input("请输入文档2路径:")
#将两个文本中的每行中的空格替换掉,形成新的字符串
str1 = ""
str2 = ""
with open(text1, "r", encoding="utf-8") as f1:
content1 = f1.readlines()
for each_line_1 in content1:
new_content1 = each_line_1.replace(" ", "")
str1 += new_content1
with open(text2, "r",encoding="utf-8") as f3:
content2 = f3.readlines()
for each_line_2 in content2:
new_content2 = each_line_2.replace(" ", "")
str2 += new_content2
# 比较文档差异
different = difflib.HtmlDiff()
html_content = different.make_file(str1, str2)
with open("文本比较.html", "w", encoding="utf-8") as f:
f.write(html_content) def main(fa, fb):
with open(fa) as f:
ta = f.readlines()
with open(fb) as f:
tb = f.readlines()
la = len(ta)
lb = len(tb)
if la != lb:
return False
for a, b in zip(ta, tb):
if a != b:
return False
else:
return True
if __name__ == "__main__":
print(main('test1.txt', 'test2.txt'))
suchocolate 发表于 2021-12-10 23:20
你写的也对,但是可能不符合我的实际需求,不光要比较不同,还要比较去看是哪里不一样, 代码小白liu 发表于 2021-12-11 08:38
你写的也对,但是可能不符合我的实际需求,不光要比较不同,还要比较去看是哪里不一样,
把你所有的需求都写出来。 本帖最后由 z5560636 于 2021-12-11 10:11 编辑
每天进步一点,查看源码后发现:
html_content = different.make_file(tolines ='to',str1, str2)
试试这个参数 tolines 这个是按行匹配。
"""Returns HTML file of side by side comparison with change highlights
Arguments:
fromlines -- list of "from" lines
tolines -- list of "to" lines
fromdesc -- "from" file column header string
todesc -- "to" file column header string
context -- set to True for contextual differences (defaults to False
which shows full differences).
numlines -- number of context lines. When context is set True,
controls number of lines displayed before and after the change.
When context is False, controls the number of lines to place
the "next" link anchors before the next change (so click of
"next" link jumps to just before the change).
charset -- charset of the HTML document
""" suchocolate 发表于 2021-12-11 09:23
把你所有的需求都写出来。
我的需求就是比较两个文档内容,然后需要去看具体是哪里不一样。如果有空格或者空行的话最好能去掉
页:
[1]