For a training project, I have many csv files that I need to change from a semicolon (,), separated by a semicolon (;), separated. Therefore, I only need to change the delimiter.
I usually do this in Excel, but it takes a lot of work. And there I need to do this for each file separately, plus Excel will take a long time to do this.
I created an input and output folder. This works well in the code below. The problem is this:
- the comma does not change to a semicolon.
- and for some reason it adds an empty string, I donβt know why it does it.
Can someone give some advice?
import csv
from pathlib import Path
folder_in = Path(r'C:\convert\Trajectory\In')
folder_out = Path(r'C:\convert\Trajectory\Out')
for incsv in folder_in.iterdir():
outcsv = folder_out.joinpath(incsv.name)
with open(str(incsv), 'r') as fin, open(str(outcsv), 'w') as fout:
reader = csv.DictReader(fin)
writer = csv.DictWriter(fout, reader.fieldnames, delimiter=';')
writer.writeheader()
writer.writerows(reader)
source
share