I'm new with python. Here is my csv file :
data;data;name surname; data; data
data;data;name surname; data; data
data;data;name surname; data; data
data;data;name surname; data; data
The thing that I want to do is to insert ";" to make name and surname 2 columns like that :
data;data;name;surname; data; data
data;data;name;surname; data; data
data;data;name;surname; data; data
data;data;name;surname; data; data
But the hard thing is sometimes there is more than one space, like that :
data;data;name surname surname2; data
data;data;name surname surname2 surname3; data
And I just want to replace the first spaces, not all like that :
data;data;name;surname surname2; data
data;data;name;surname surname2 surname3; data
Here is my code but it replaces every space:
def modify_rows():
with open("result2.csv","rb") as source:
rdr= csv.reader(source, delimiter=';')
with open("result3.csv","wb") as result:
wtr= csv.writer(result,delimiter=';')
for r in rdr:
rowname = r[3].replace(' ', ';')
wtr.writerow((r[0],r[1],rowname,r[2]))
Hope I can find help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…