python文本比对


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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM