#!/usr/bin/env python
import csv
file1reader = csv.reader(open("mapping.csv"), delimiter=",")
header1 = file1reader.next() #header
header2 = file1reader.next() #header
data = dict()
result=[]
row=[]
for Key, Prop, Val, Flag in file1reader:
if key in data: # found it
x=data[Key]
data[Key] = x + "," + Val
row.append(Val)
else:
data[Key] = Val
result.append(row)
row=[]
row.append(Key)
row.append(Val)
with open('myfile.csv', 'wb') as f:
w = csv.writer(f,dialect='excel')
w.writerows(result)
Example mapping.csv file:
Key1, prop1, value1
Key1, prop2, value2
Key1, prop3, value3
Key2, prop1, value4
Key2, prop2, value5
Key2, prop3, value6
Output file:
Key1, value1, value2, value3
Key2, value3, value4, value5
import csv
file1reader = csv.reader(open("mapping.csv"), delimiter=",")
header1 = file1reader.next() #header
header2 = file1reader.next() #header
data = dict()
result=[]
row=[]
for Key, Prop, Val, Flag in file1reader:
if key in data: # found it
x=data[Key]
data[Key] = x + "," + Val
row.append(Val)
else:
data[Key] = Val
result.append(row)
row=[]
row.append(Key)
row.append(Val)
with open('myfile.csv', 'wb') as f:
w = csv.writer(f,dialect='excel')
w.writerows(result)
Example mapping.csv file:
Key1, prop1, value1
Key1, prop2, value2
Key1, prop3, value3
Key2, prop1, value4
Key2, prop2, value5
Key2, prop3, value6
Output file:
Key1, value1, value2, value3
Key2, value3, value4, value5
No comments:
Post a Comment