可以用 difflib库,下面给一个例子,具体需求自己研究
假如在同一个目录下有a.txt, b.txt 两个文本文件
a.txt 内容是
aaa
bbb
b.txt内容是
aaa
ccc
import diffliba = open('a.txt', 'U').readlines()
b = open('b.txt', 'U').readlines()
diff = difflib.ndiff(a, b)
sys.stdout.writelines(diff)
结果是:
aaa
- bbb+ ccc
使用xlrd和xlwt包,首先安装这两个包。定义contrast函数,测试contrast函数,把程序打包成exe文件。导入tkinter包,写个函数用来选择路径,初始化变量,画出UI界面,点击对比按钮后的函数。接下来就是把这个py程序打包,使用pyinstaller这个包pipinstallpyinstaller。安装成功之后,按键盘win+R打开运行,输入cmd,回车运行。进入程序所在文件夹。
因为有统计成员到会情况的任务,每次汇总时都很麻烦,需要一个个对应腾讯会议导出名单的成员,然后在总表上进行标记,所以就写了本程序来减少统计的复杂度。
这里假设题目里面提到的”单独两个JSON文件的比较方法“的函数是compare_two_files,它接受两个文件的文件名作为参数。
from pathlib import Path
def compare_two_folders(from_folder, to_folder):
from_folder = Path(from_folder)
to_folder = Path(to_folder)
for json_file in from_folder.glob('*.json'):
json_file_name = json_file.name
json_file_to_compare = to_folder / f'a{json_file_name}'
compare_tow_files(json_file, json_file_to_compare)