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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.