0

I realise similar has been posted before 1, 2, 3

but I have tried these and they have not worked.

I have a single csv file with two columns similar to this:

james,phone1
james,phone2
james,phone3
paul,phone1
jackie,phone1
jackie,phone2
jackie,phone3
etc

I want to merge all the duplicates in column 1 using python to get something like:

james,phone1,phone2,phone3
paul,phone1
jackie,phone1,phone2,phone3

What would be the best way of doing this?

Any help would be greatly appreciated.

Community
  • 1
  • 1

1 Answers1

0
import csv
filename = "Filename.csv"
csvList = list(csv.reader(open(filename)))
csvDict = {}
for i in csvList :
    if i[0] in csvDict :
        csvDict[i[0]].append(i[1])
    else :
        csvDict[i[0]] = [i[1]]

print(csvDict)

use the dictionary to create the appropriate output format.

Harry
  • 320
  • 1
  • 9
  • Glad to be helpful. Up-voting the answer and accepting the answer will be helpful for me. :) – Harry Jan 29 '17 at 19:12