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

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -