I need to make some changes to my csv.
I seek to separate the rows that I have in country by the "/" and duplicate them.
Basically I explain a little because I need to do this:
When I have a "/" in the row of Country
it means that they are both countries and for technical reasons I need to have only one country per row.
example:
*,CA - Canada/UK - United Kingdom,0.45
output:
*,CA - Canada,0.45
*,UK - United Kingdom,0.45
Data:
Space,Country,Price
*,AR - Argentina,0.3
*,CA - Canada/UK - United Kingdom,0.45
*,CL - Chile/PE - Peru,0.25
*,CO - Colombia/EC - Ecuador,0.15
*,CR - Costa Rica/ES - Spain,0.2
*,DE - Germany,0.5
*,MX - Mexico,0.4
*,US - United States,0.8
What I'm looking for:
Space,Country,Price
*,AR - Argentina,0.3
*,CA - Canada,0.45
*,UK - United Kingdom,0.45
*,CL - Chile,0.25
*,PE - Peru,0.25
*,CO - Colombia,0.15
*,EC - Ecuador,0.15
*,CR - Costa Rica,0.2
*,ES - Spain,0.2
*,DE - Germany,0.5
*,MX - Mexico,0.4
*,US - United States,0.8
Code:
import csv
with open("pre.csv", 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
print your_list
divididos = []
for item2 in your_list:
if "/" in item2[1]:
a = item2[1].split('/')
print item2[0] + ',' + a[0] + ',' + item2[2] + ',' + item2[3]
print item2[0] + ',' + a[1] + ',' + item2[2] + ',' + item2[3]
else:
print item2