mardi 1 août 2017

How can make merge data using for loop?

I tried merge two file.

a.txt file has below 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

b.txt file has below 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 want 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;

I can't make correct result. my script make too many line. who can check my script. I just begin python study. please help me. Below is my script.

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 + ";"

                        s.write(result + '\n')

s.close()

Aucun commentaire:

Enregistrer un commentaire