How do I make a search key and combine the data using a loop?
I tried to merge two files. I want to find the same text Aliase on match b.txt on Alias1 on a.txt. I want to see below output:
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 the 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;
The A.txt file contains the following data:
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
The B.txt file contains the following data:
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 seem to get the correct result. I am just starting to learn python. please help me.
Here's 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()
+3
source to share
No one has answered this question yet
See similar questions:
or similar: