python - replacing all but first commas with blank value -



python - replacing all but first commas with blank value -

i have csv in format:

a, hello, name john, how you? b, hello john, i'm doing well, thanks!

right now, csv shows 4 values first , sec entry since both delimited commas. goal have csv show 2 values each entry so:

a, hello name john how you? b, hello john i'm doing thanks!

i tried replace function converts every single comma space. wondering if there such thing skip first comma , have every comma (after first one) replaced "". here's have far:

fin = open ('example.csv', 'r+') fout = open ('newexample.csv', 'w') reader = csv.reader(fin) line in reader: in range(1, len(line)): line[i].replace(',', ''); fout.write(line); fin.close() fout.close()

i don't mind if solution doesn't follow below code, whichever fine, long produces desired output

thanks in advance!

>>> line = 'a, hello, name john, how you?' >>> head, tail = line.split(',', 1) >>> head 'a' >>> tail 'hello, name john, how you?' >>> tail.replace(',', ' ') 'hello name john how you?' >>> ','.join([head, tail.replace(',', ' ')])) 'a, hello name john how you?'

python csv replace

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -