如何比较两个不同的列数据,无论不同文本文件中的顺序如何?

时间:2015-12-16 02:30:39

标签: python arrays

想要这样做,

输入1(文本文件1)

ABCD,  QI30  
FGDE,  Q140  
KLOM,  Q232   
..      ..  
..      ..   
..      ..

输入2(文本文件2)

DATA: ARRAY: KLOM: n: 000000000111111110011111111111111111111111  
DATA: ARRAY: ABCD: n: 000111111111111111101111111111111111111111  
DATA: ARRAY: FGDE: n: 010101010111111111111111111111111111111111  
..      ..  
..      ..   
..      ..

输出(写入文本文件3)

Item=Q232; Var:KLOM    
read_Var:read_q232: Get_Data: n: 000000000111111110011111111111111111111111  

Item=Q130; Var:ABCD  
read_Var:read_q150: Get_Data: n: 000111111111111111101111111111111111111111  

Item=Q140; Var:FGDE  
read_Var:read_q170: Get_Data: n: 010101010111111111111111111111111111111111   
..      ..  
..      ..   
..      ..

这是代码的一部分,

import collections

#Create a data dict using textfile1.txt
with open("textfile1.txt") as f1:   
    # create generator of entries using non-empty lines in file
    entries1 = (line.split() for line in f1 if line.strip())
    # create dict using ($1) as key and $2 as value
    data1 = dict(((d[0], d[1]) for d in entries1)

with open("textfile2.txt") as f2:   
    entries2 = (line.split() for line in f2 if line.strip())
    data2 = dict(((f[2], f[3]), f[4]) for f2 in entries2)

   set(d[1]) == set(f[2]) # compare the text file1 column 2 with text file2 column 3 

在此之后,我不知道该怎么做。真的,我需要你帮忙。

1 个答案:

答案 0 :(得分:3)

这会产生所需的输出:

ids = {}
with open('input1.txt') as fobj:
    for line in fobj:
        if line.strip():
            key, value = [x.strip() for x in line.split(',')]
            ids[key] = value

arrays = []
with open('input2.txt') as fobj:
    for line in fobj:
        if line.strip():
            values = [x.strip() for x in line.split(':')]
            arrays.append((values[2], values[3:]))

with open('output.txt', 'w') as fobj:
    for key, values in arrays:
        item = ids[key]
        fobj.write('Item={}; Var:{}\n'.format(item, key))
        fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),
                                                            ':'.join(values)))