首页 > 编程语言 > 详细

python文本比对

时间:2021-07-28 18:32:32      阅读:19      评论:0      收藏:0      [点我收藏+]

1.说明

文本比对脚本(可比对IP,域名等文本)
set1_ip.txt 为旧文件
set2_ip.txt 为新文件
start_py.bat 为启动脚本

技术分享图片

结果显示说明

delete:  为新文件里面没有,但旧文件里面有

add:  为新文件里面有,旧文件里面没有的

unchanged:  为新旧文件都存在的

2.代码

新建start_py.bat 批处理文件,写入一下批处理代码(当前使用环境是python3.8.0,可根据实际环境更改)

title 文本比对脚本
%cd%\python-3.8.0\python.exe diff_txt.py set1_ip.txt set2_ip.txt
pause

diff_txt.py 比对脚本文件代码如下:

# -*- coding:UTF-8 -*-
import sys

def diff_content(set1,set2):
    common_set = set1 & set2
    only1_set = set1 - set2
    only2_set = set2 - set1
    print "delete:"
    for j in list(only1_set):
        print j
    print "\n\n"
    print "add:"
    for h in list(only2_set):
        print h
    print "\n\n"
    print "unchanged:"
    for i in list(common_set):
        print i

def read_file(filename):
    content_set = set()
    with open(filename,‘r‘) as f:
        for line in f:
            line= line.replace(" ","").replace(‘\n‘,‘‘)
            content_set.add(line)
    return content_set

if __name__ == "__main__" :
    set1_file=read_file(sys.argv[1])
    set2_file=read_file(sys.argv[2])
    diff_content(set1_file,set2_file)

python文本比对

原文:https://www.cnblogs.com/kepp/p/15070398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!