使用python比较文本行(多个列表)

时间:2013-04-10 20:50:41

标签: python

我的文本文件包含以下行:

Cycle 0 DUT 2 Bad Block : 2,4,6,7,8,10,12,14,16,18,20,22,24,26,28
Cycle 0 DUT 3 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26
Cycle 0 DUT 4 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26
Cycle 1 DUT 2 Bad Block : 2,4,6,7,8,10,12,14,16,18,20,22,24,26,28
Cycle 1 DUT 3 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26,28,30,32

我想将Cycle 0 DUT 2文本行(用逗号分隔冒号后的数字)与Cycle 1 DUT 2文本行(冒号用逗号分隔后的数字)进行比较并得到差异,然后比较{{1将文本行添加到Cycle 0 DUT 3文本行并获取差异或唯一值。

1 个答案:

答案 0 :(得分:1)

我想你想把关键字改为DUT数字:

import re
dut_data = {}

cycle_dut = re.compile('^Cycle\s+(\d)\s+DUT\s+(\d)\s+Bad Block\s*:\s*(.*)$')

with open(inputfile, 'r') as infile:
    for line in infile:
        match = cycle_dut.search(line)
        if match:
            cycle, dut, data = match.groups()
            data = [int(v) for v in data.split(',')]
            if cycle == '0':
                # Store cycle 0 DUT values keyed on the DUT number
                dut_data[dut] = data
            else:
                # Compare against cycle 0 data, if the same DUT number was present
                cycle_0_data = dut_data.get(dut)
                if cycle_0_data is not None:
                    # compare cycle_0_data and data here
                    print 'DUT {} differences: {}'.format(dut, ','.join([str(v) for v in sorted(set(cycle_0_data).symmetric_difference(data))]))

我使用快速设置差异来打印差异,这可能需要精炼。

对于您的样本数据,打印:

DUT 2 differences: 
DUT 3 differences: 28,30,32