1.效果

2.功能

1.找出两个文本中不同的地方
2.统计两个文本各自的字数(含符号与不含符号)
3.统计两文本之间相对重复率(含符号与不含符号)

3.代码

3.1 制定界面

用QTdesigner制作界面,保存成.ui文件。

回到Pycharm里用pyuic5转换为python代码:

pyuic5 compare.ui -o compare.py

转化完成的界面代码:

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'compare_2.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(1114, 830)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)self.plainTextEdit.setGeometry(QtCore.QRect(290, 10, 781, 301))self.plainTextEdit.setObjectName("plainTextEdit")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(810, 610, 211, 34))self.pushButton_2.setObjectName("pushButton_2")self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_3.setGeometry(QtCore.QRect(370, 610, 191, 34))self.pushButton_3.setObjectName("pushButton_3")self.plainTextEdit_4 = QtWidgets.QPlainTextEdit(self.centralwidget)self.plainTextEdit_4.setGeometry(QtCore.QRect(290, 320, 781, 281))self.plainTextEdit_4.setObjectName("plainTextEdit_4")self.webEngineView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)self.webEngineView.setGeometry(QtCore.QRect(10, 10, 261, 761))self.webEngineView.setUrl(QtCore.QUrl("about:blank"))self.webEngineView.setObjectName("webEngineView")self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)self.textBrowser.setGeometry(QtCore.QRect(290, 660, 781, 111))self.textBrowser.setObjectName("textBrowser")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 1114, 30))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton_2.setText(_translate("MainWindow", "比较"))self.pushButton_3.setText(_translate("MainWindow", "清空"))from PyQt5 import QtWebEngineWidgets

3.2 程序

from sys import exit,argv
import difflib
import compare_ui2
import re## 比较函数
def compare_file(file1, file2):file1_content = file1file2_content = file2file1_content = file1_content.replace("\n", '').replace("\t", '')file2_content = file2_content.replace("\n", '').replace("\t", '')d = difflib.HtmlDiff()result = d.make_file(file1_content, file2_content)return result## 绑定动作函数
def clicked():text1 = ui.plainTextEdit.toPlainText()text2 = ui.plainTextEdit_4.toPlainText()result=compare_file(text1, text2)# 要让html在PyQT界面显示,需要在html前后加上三引号f = "'''" + result + "'''"ui.webEngineView.setHtml(f)calculate()## 清空函数
def clean():ui.plainTextEdit.clear()ui.plainTextEdit_4.clear()ui.textBrowser.clear()##计数函数
def count_all():text1 = ui.plainTextEdit.toPlainText()text2 = ui.plainTextEdit_4.toPlainText()com = difflib.Differ()com = com.compare(text1, text2)str = "".join(com)text1_total=len(text1)text2_total=len(text2)text1_only = str.count("-")text2_only=str.count("+")return text1_only,text1_total,text2_only,text2_total##计数函数(不含符号)
def count_ca():text1 = ui.plainTextEdit.toPlainText()text2 = ui.plainTextEdit_4.toPlainText()#去掉文字以外的符号text1 = re.sub("[\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+", "", text1)text2 = re.sub("[\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+", "", text2)com = difflib.Differ()com = com.compare(text1, text2)str = "".join(com)text1_total=len(text1)text2_total=len(text2)text1_only=str.count("-")text2_only=str.count("+")return text1_only, text1_total, text2_only, text2_total## 统计两文本的详细信息
def calculate():text1_only, text1_total, text2_only, text2_total=count_all()text1_caonly, text1_catotal, text2_caonly, text2_catotal=count_ca()try:text1_repetitive_rate=(text1_total-text1_only)/text1_total*100text2_repetitive_rate=(text2_total-text2_only)/text2_total*100text2ca_repetitive_rate=(text2_catotal-text2_caonly)/text2_catotal*100text1ca_repetitive_rate=(text1_catotal-text1_caonly)/text1_catotal*100ui.textBrowser.append("文本1总字数:" + str(text1_total))ui.textBrowser.append("文本2总字数:" + str(text2_total))ui.textBrowser.append("去掉所有符号后,文本1总字数:" + str(text1_catotal))ui.textBrowser.append("去掉所有符号后,文本2总字数:" + str(text2_catotal))ui.textBrowser.append("文本1独有字符数:" + str(text1_only))ui.textBrowser.append("文本2独有字符数:" + str(text2_only))ui.textBrowser.append("文本1相对于文本2的重复率为:" + str(text1_repetitive_rate) + "%")ui.textBrowser.append("文本2相对于文本1的重复率为:" + str(text2_repetitive_rate) + "%")ui.textBrowser.append("去掉所有符号后,文本2相对于文本1的重复率为:" + str(text2ca_repetitive_rate) + "%")ui.textBrowser.append("去掉所有符号后,文本1相对于文本2的重复率为:" + str(text1ca_repetitive_rate) + "%")except Exception as e:ui.textBrowser.append("文本1或文本2为空;或其中一个文本仅有符号,没有字:" + str(e))## 主程序
if __name__ == '__main__':app = compare_ui2.QApplication(argv)widgets = compare_ui2.QMainWindow()ui = compare_ui2.Ui_MainWindow()ui.setupUi(widgets)try:if  ui.pushButton_2.isDown:ui.pushButton_2.clicked.connect(clicked)if ui.pushButton_3.isDown:ui.pushButton_3.clicked.connect(clean)except Exception as e:ui.textBrowser.append("错误:" + str(e))widgets.show()exit(app.exec_())

4.程序打包

在Pycharm里使用pyinstaller打包:

pyinstaller -w compare.py

-w: 隐藏后台黑框
-f: 打包成一个文件,没有这个命令则打包成文件夹

我打包好的文件,可以下载直接使用:

链接: https://pan.baidu.com/s/17URrtWpY35WMZ0RAwxKHJQ
提取码: bryw

python查找两文本不同字符及其相对重复率等及其pyqt5界面相关推荐

  1. 解决:Word需要查找两个固定字符间的字符 Word将查找到的字符全部选中

    问题描述: 1.Word需要查找两个固定字符间的字符.例如:[1].[tttttt].[nice and well]等. 2.将查找到的字符全部选中,然后复制. 解决方案: 问题1解决方案: 在Wor ...

  2. python怎么筛选excel数据_python筛选数据excel表格-如何利用python提取两个excel对比后的重复值的信息?...

    怎么用python读取excel表格的数据 import xlrd #open the .xls file xlsname="test.xls" book = xlrd.open_ ...

  3. Python 查找字符串内所有字符起始位置

     定义: def strfindall(zstr, xstr):"""查找字符串内所有字符起始位置\n:param zstr: 被查找的字符串容器:param xstr: ...

  4. Python | 查找字符串中每个字符的频率

    Given a string and we have to find the frequency of each character of the string in Python. 给定一个字符串, ...

  5. Python查找两个word中的相同内容

    参考链接:https://blog.csdn.net/weixin_43145361/article/details/103798581 参考链接:https://zhidao.baidu.com/q ...

  6. python查找两个数组中相同的元素_找出两个数组的相同元素,最优算法?

    在做新旧接口交替过程中,遇到了老接口和新接口json数据有些不一致的情况,需要比较两个json对象,把相同的元素赋其中一个json对象中变量的值.而且其中一个json最后输出格式还需要改变下属性名,思 ...

  7. python查找两个数组中相同的元素_匹配两个numpy数组以找到相同的元素

    使用熊猫:import pandas as pd id1 = pd.read_csv('id1.txt') id2 = pd.read_csv('id2.txt') df = id1.merge(id ...

  8. python图片转文本(字符画)

    from PIL import Image serarr=['@','#','$','%','&','?','*','o','/','{','[','(','|','!','^','~','- ...

  9. python 查找两列不同的值、相同的值(dataframe数据探索)

    在做数据挖掘时,查看训练集.测试集数据的情况时,有时需要查看两者之间不同的值和相同的值. import pandas as pd import numpy as np data1 = pd.DataF ...

  10. python查找指定字符所在行号_python查找字符串中某个字符

    本文收集整理关于python查找字符串中某个字符的相关议题,使用内容导航快速到达. 内容导航: Q1:Python里统计一个字符串中另一个字符串的个数 答案为3(用正则):1234>>&g ...

最新文章

  1. python汉字转到ascii码_python中字母与ascii码的相互转换
  2. Loadrunner压测时,出现的问题汇总
  3. linux通过进程名查找进程,Linux下通过进程名获得进程号
  4. [导入]浅析.Net下的AppDomain编程
  5. 《机器学习》 周志华学习笔记第五章 神经网络(课后习题) python实现
  6. Java中的NIO非阻塞编程
  7. 离散余弦变换原理及实现过程【转载】
  8. Python入门5_条件循环语句
  9. java整段标记_聊聊JAVA GC系列(7) - 标记整理算法
  10. 第十五回(二):文会内战平分秋色 树下阔论使坏心焦【林大帅作品】
  11. 30轧制过程的计算机控制系统,中厚板轧制过程计算机控制系统结构的研制(1)
  12. 认识Hibernate
  13. Protel99SE教程(一)——原理图封装
  14. 三行代码让你轻松下载全网任意视频-Python小知识
  15. VM296:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (anonymous)
  16. sai文字图层、钢笔图层如何转普通图层
  17. 鲁大师2021年度PC硬件报告:AMD跑分超神,华米OV入局笔记本
  18. 微信公众号群发功能的页面元素加载不全的解决办法
  19. 表征学习 Representation Learning(特征学习、表示学习)是什么?
  20. Python编程零基础如何逆袭成为爬虫实战高手

热门文章

  1. halcon 条形码识别(持续更新)
  2. html表格筛选排序规则,excel表格的排序规则与排序技术
  3. 8类网线利弊_超6类7类8类网线进来挨打 6类线全面测评 网速和传输速率测试
  4. 【linux】什么是栈回溯
  5. 希捷服务器硬盘有什么用,NAS储存有什么用?配置什么硬盘?
  6. 图表排版设计html,网页的排版(表格篇上)
  7. 计算方法 6.插值法
  8. 《AI·未来》 ---- 读书笔记
  9. c语言小游戏跳一跳代码及注释,c语言小游戏程序之弹跳小球的实现代码
  10. web前端之跳一跳网页版小游戏