Reputation: 417
I have something like the following CSV:
"blah blah, foo bar", 1, 454, ok, eng
blah blah foo bar, 2, 21, ko, esp
...
I need to replace the commas between fields into semicolons so I can split correctly the CSV. But don't the ones inside the quoutes.
I need or a regex o a little script(python, groovy, bash...) to fix this.
Upvotes: 0
Views: 778
Reputation: 78680
Consider using Python's csv
module. Demo:
from csv import reader, writer
with open('input.csv') as inp, open('output.csv', 'w') as out:
writer(out, delimiter=';').writerows(reader(inp))
file contents:
$ cat input.csv
"blah blah, foo bar", 1, 454, ok, eng
blah blah foo bar, 2, 21, ko, esp
$ cat output.csv
blah blah, foo bar; 1; 454; ok; eng
blah blah foo bar; 2; 21; ko; esp
Upvotes: 2
Reputation: 744
import csv
infile = open("inputfile.csv", "r")
outfile = open("outputfile.csv", "w")
csvReader = csv.reader(infile)
for line in csvReader:
outfile.write(";".join(line) + "\n")
infile.close()
outfile.close()
Tested on Python 2.7 and 3.5
Output on your example:
blah blah, foo bar; 1; 454; ok; eng
blah blah foo bar; 2; 21; ko; esp
Upvotes: 1