Skip to content

Instantly share code, notes, and snippets.

@cindygis
Created July 19, 2016 07:57
Show Gist options
  • Save cindygis/7612ce60717be44409fa1aec650caed8 to your computer and use it in GitHub Desktop.
Save cindygis/7612ce60717be44409fa1aec650caed8 to your computer and use it in GitHub Desktop.
Replaces the delimiter in a csv file
'''
@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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment