比较并筛选2个文件

2024-09-29 21:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图比较和过滤两个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()))
 .
 .
 .

Tags: 文件theinimportsendlinechannelfunction
1条回答
网友
1楼 · 发布于 2024-09-29 21:28:47

首先,你需要问问自己;您是在比较代码,还是在比较行?如果要比较代码,就需要考虑空格、允许反向函数顺序、缩进差异等方面而言,这会变得很棘手。如果要比较代码,最好使用Python的ast模块来比较部分

如果您严格比较行,那么可以节省大量工作,并使用外部库,如deepdiff(无关联)。我想它会给你你所需要的大部分东西

下面是一个使用deepdiff(按原样工作)的示例

pip安装deepdiff

from pprint import pprint
from textwrap import dedent
from deepdiff import DeepDiff

file1 = dedent('''
 [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()
''').split("\n")

file2 = dedent('''
 [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()
''').split("\n")


if __name__ == '__main__':
    diff = DeepDiff(file1, file2)
    pprint(diff)

输出

{'values_changed': {'root[10]': {'new_value': '[10] return channel2.receive()',
                                 'old_value': '[10] return channel.receive()'},
                    'root[5]': {'new_value': '[5] % (Module, Function))',
                                'old_value': '[5] channel = '
                                             'gw.remote_exec("""'},
                    'root[6]': {'new_value': '[6] from %s import %s as '
                                             'the_function',
                                'old_value': '[6]  from %s import %s as '
                                             'the_function'},
                    'root[7]': {'new_value': '[7] '
                                             'channel2.send(the_function(*channel2.receive()))',
                                'old_value': '[7]  '
                                             'channel.send(the_function(*channel.receive()))'},
                    'root[8]': {'new_value': '[8] channel2 = '
                                             'gw.remote_exec("""',
                                'old_value': '[8] """ % (Module, Function))'},
                    'root[9]': {'new_value': '[9] channel2.send(ArgumentList)',
                                'old_value': '[9] channel.send(ArgumentList)'}}}

相关问题 更多 >

    热门问题