How can make search key and merge data using for loop?

Viewed 72

I tried to merge two files. I want to find same text Aliase on b.txt match on Alias1 on a.txt. I want to see below result:

VNX7600SPB3_8B3_H1;VNX7600SPB3;39;vv:06:01:xx:08:60:xx:78;8B3_H1;1
VNX7600SPBA_8B4_H1;VNX7600SPA3;35;xx:06:01:vv:08:60:vv:78;8B4_H1;2
CX480SPA1_11B3_H1;CX480SPA1;N/A;11B3_H1;
CX480SPB1_11B4_H1;CX480SPB1;N/A;11B4_H1;

But now myscript result is below:

VNX7600SPB3_8B3_H1;VNX7600SPB3;39;vv:06:01:xx:08:60:xx:78;8B3_H1;
VNX7600SPB3_8B3_H1;VNX7600SPB3;N/A;8B3_H1;
CX480SPB1_11B4_H1;CX480SPB1;N/A;11B4_H1;
CX480SPB1_11B4_H1;CX480SPB1;N/A;11B4_H1;
VNX7600SPBA_8B4_H1;VNX7600SPA3;N/A;8B4_H1;
VNX7600SPBA_8B4_H1;VNX7600SPA3;35;xx:06:01:vv:08:60:vv:78;8B4_H1;
CX480SPA1_11B3_H1;CX480SPA1;N/A;11B3_H1;
CX480SPA1_11B3_H1;CX480SPA1;N/A;11B3_H1;

A.txt file has the data below:

Zone,Alias1,Alias2
VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1
VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1
CX480SPA1_11B3_H1,CX480SPA1,11B3_H1
CX480SPB1_11B4_H1,CX480SPB1,11B4_H1

B.txt file has the data below:

WWN,Port,Aliase
xx:06:01:vv:08:60:vv:78,35,VNX7600SPA3
vv:06:01:xx:08:60:xx:78,39,VNX7600SPB3

I can't make the correct result. I just begin python study. please help me.

Here is my script too:

f = open('b.txt', 'r')
dict = {}
next(f)
i=0
for line in f:
        i += 1
        line = line.split(',')
        line = (line[2].replace('\n','') +',' + line[1] + ";" + line[0]).strip()
        if line.startswith(",") == 1:
                line = str("Blank") + str(i) + line
        else:
                line = line

        k, v = line.strip().split(',')
        dict[k.strip()] = v.strip()
f.close()


f = open('a.txt','r')
next(f)
with open('result.txt','w') as s:
        for lines in f.xreadlines():
                lines = lines.split(',')
                zone = lines[0]
                ali1 = lines[1]
                ali2 = lines[2].replace('\n','')

##I think below line make same line and no need line


                for eachkey in dict.keys():
                        if eachkey in ali1:
                                result = zone + ";" + ali1 + ";" + dict[eachkey] + ";" + ali2 + ";"

                        elif eachkey not in ali1:
                                result = zone + ";" + ali1 + ";" + "N/A"  + ";" + ali2 + ";"
                        else:
                                continue
                        s.write(result + '\n')

s.close()
0 Answers
Related