簡體   English   中英

兩個文件的區別

[英]Differences between two files

我正在連接到遠程機器,並將兩個查詢的輸出保存在兩個不同的文件中,並將結果作為今天出現的新結果:

有兩個文件:

昨天.txt

AAA=1
BBB=2
CCC=3

今天.txt

AAA=1
BBB=2
DDD=3
EEE=2

腳本的輸出應該是這樣的:

WARNING: DIFFERENCES: DDD=3, EEE=2;

我怎么能接受這種差異?

我已經嘗試使用 Python 腳本中的“diff”命令,但它似乎有問題......

使用Difflib庫:

from difflib import Differ

# AAA=1
# BBB=2
# CCC=3
with open("yeterday.txt","r") as f1:
    text1 = f1.read()

# AAA=1
# BBB=2
# DDD=3
# EEE=2
with open("today.txt","r") as f2:
    text2 = f2.read()


d = Differ()
result = list(d.compare(text1, text2))


#   A  A  A  =  1  
#   B  B  B  =  2  
# - C- C- C+ D+ D+ D  =  3+ 
# + E+ E+ E+ =+ 2
print(''.join(result))

# Printing the desired message if difference was detected
output_list = ''.join([diff_char.replace('+ ', '').replace(' +', '') for diff_char in difflib.ndiff(text1, text2) if '+' in diff_char[0]])

if output_list:
    # WARNING: DIFFERENCES:
    # DDD
    # EEE=2
    print(f'WARNING: DIFFERENCES:\n{output_list}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM