我试图比较和过滤两个dat文件的差异。更具体地说,有两个文件,每个文件大约有2000行代码。这两个文件有很多共同点,所以我试图过滤不同的部分。我做了第一部分,比较两个文件,但我需要过滤它们,因为比较是逐行进行的。例如,file1.dat在第100行中有include,file2.dat在第102行中有相同的include。所以逐行比较并不能帮助我进行过滤
这是我的剧本:
import re
filename = "{0}_{1}.txt"
name = filename.format(f'{"TB"}', f'{"Differences"}')
# reading files
f = open(name,'w')
f1 = open("file1.dat", "r")
f2 = open("file2.dat", "r")
i = 0
#######COLORS#######
CRED = '\033[91m'
CGREEN = '\33[32m'
CEND = '\033[0m'
####################
for line1 in f1:
i += 1
line1 = re.sub(" +", " ", line1)
for line2 in f2:
line2 = re.sub(" +", " ", line2)
# matching line1 from both files
if line1 == line2:
print(" Line ", i, ": PASS")
f.write("PASS\n")
else:
print(CGREEN + " Line ", i, ":" + CEND)
# else print that line from both files
print(CRED + "\tFAIL" + CEND)
print("\tFile 1:", line1, end='')
print("\tFile 2:", line2, end='')
f.write(f"Difference found in Line {i}\n ")
f.write(f"Line {i} in {f1}--> {line1} ")
f.write(f"Line {i} in {f2}--> {line2}")
break
# closing files
f1.close()
f2.close()
f.close()
样本: 输入文件1.dat具有:
[1] import execnet
[2]
[3] def call_python_version(Version, Module, Function, ArgumentList):
[4] gw = execnet.makegateway("popen//python=python%s" % Version)
[5] channel = gw.remote_exec("""
[6] from %s import %s as the_function
[7] channel.send(the_function(*channel.receive()))
[8] """ % (Module, Function))
[9] channel.send(ArgumentList)
[10] return channel.receive()
输入文件2.dat具有:
[1] import execnet
[2]
[3] def call_python_version(Version, Module, Function, ArgumentList):
[4] gw = execnet.makegateway("popen//python=python%s" % Version)
[5] % (Module, Function))
[6] from %s import %s as the_function
[7] channel2.send(the_function(*channel2.receive()))
[8] channel2 = gw.remote_exec("""
[9] channel2.send(ArgumentList)
[10] return channel2.receive()
输出txt有:
file1 = channel = gw.remote_exec("""
file2 = channel2 = gw.remote_exec("""
file1 = channel.send(the_function(*channel.receive()))
file2 = channel2.send(the_function(*channel2.receive()))
.
.
.
首先,你需要问问自己;您是在比较代码,还是在比较行?如果要比较代码,就需要考虑空格、允许反向函数顺序、缩进差异等方面而言,这会变得很棘手。如果要比较代码,最好使用Python的ast模块来比较部分
如果您严格比较行,那么可以节省大量工作,并使用外部库,如deepdiff(无关联)。我想它会给你你所需要的大部分东西
下面是一个使用deepdiff(按原样工作)的示例
pip安装deepdiff
输出
相关问题 更多 >
编程相关推荐