简体   繁体   中英

compare text lines(multiple lists) using python

I have the text file which contains below lines:

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

I want to compare the Cycle 0 DUT 2 text line (numbers after colon separated with commas) to the Cycle 1 DUT 2 text line (numbers after colon seperated with commas) and get the differences, then compare Cycle 0 DUT 3 text line to Cycle 1 DUT 3 text line and get the differences or the unique values.

I guess you want to key things to to the DUT digit:

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))]))

I used a quick set difference to print the differences, this may require refining.

For your sample data, this prints:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM