Recently I had to wrangle some csv files, including some data calculations and outputting a semi-colon delimited file instead of a comma-delimited file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@date 19/07/2016 | |
@author Cindy Williams-Jayakumar | |
Replaces the delimiter in a csv file | |
using the pathlib library in Python 3 | |
''' | |
import csv | |
from pathlib import Path | |
folder_in = Path(r'C:\Some\Arb\Folder\in') | |
folder_out = Path(r'C:\Some\Arb\Folder\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) |