lundi 31 juillet 2017

How can make result no duplication?

I have a two file. I tried merge two file. But my script make same line.

a.txt file has below data.

Zone,alias1,alias2
PA1_H1,PA1,H1
PA3_H1,PA1,H1

b.txt file has below data.

WWN,Port,Aliases
da,0,PA1
dq,1, 
d2,3,H1
d4,1,

My result is below.

PA1_H1;PA1;0;da;H1;
PA1_H1;PA1;N/A;H1; <----I want this data
PA3_H1;PA1;0;da;H1;
PA3_H1;PA1;N/A;H1; <----I want this data
PA2_H2;PA2;N/A;H2;
PA2_H1;PA2;N/A;H1;

Below is my script. my script make no need line because for. Who can solved this issue?

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