0

I wish to write a Python script which reads from a csv. The csv comprises of 2 columns. I want the script to read through the first column row by row and find the corresponding value in the second row. If it finds the value in the second row I want it to input a value into a third column.

example of output

Any help with this would be much appreciated and I hope my aim is clear. Apologies in advance if it is too vague.

sharpie
  • 1
  • 1
  • 3
    Welcome to StackOverflow. The idea here is not to get people to write the code for you, but instead to get help places you get stuck. Tell us about what have you already tried and where you ran into problems. – acrosman Jul 23 '16 at 21:58
  • You could use pandas to read csv as a table. Then use group by with string concatanation like here: http://stackoverflow.com/questions/17841149/pandas-groupby-how-to-get-a-union-of-strings and then join the original table and the new table. If you are interested I can write a code as an answer. – keiv.fly Jul 23 '16 at 22:01

1 Answers1

0

this script read test.csv file and parse it an write to OUTPUT.txt

f = open("test.csv","r")
d={}
s={}
for line in f:
        l=line.split(",")
        if not l[0] in d:
                d[l[0]]=l[1].rstrip()   
                s[l[0]]=''
        else:
                s[l[0]]+=str(";")+str(l[1].rstrip())

w=open("OUTPUT.txt","w")
w.write("%-10s %-10s %-10s\r\n" % ("ID","PARENTID","Atachment"))
for i in d.keys():
        w.write("%-10s %-10s %-10s\r\n" % (i,d[i],s[i]))

f.close()
w.close()

example:

input:

1,123
2,456
1,333
3,
1,asas
1,333
000001,sasa
1,ss
1023265,333
0221212,
000001,sasa2
000001,sas4

OUTPUT:

ID         PARENTID   Atachment 
000001     sasa       ;sasa2;sas4
1023265    333                  
1          123        ;333;asas;333;ss
3                               
2          456                  
0221212  
Community
  • 1
  • 1
Baba
  • 852
  • 1
  • 17
  • 31
  • thanks @Babyy, could you please explain the if not and else parts. I am unclear what exactly the d and s mean and what exactly 's[l[0]]+=str(";")+str(l[1].rstrip())' is doing. – sharpie Jul 26 '16 at 20:47