Skip to content

Instantly share code, notes, and snippets.

@ragnarheidar
Last active July 8, 2021 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragnarheidar/89563c531a15aa5c3f5e to your computer and use it in GitHub Desktop.
Save ragnarheidar/89563c531a15aa5c3f5e to your computer and use it in GitHub Desktop.
A python script that reads a CSV file and writes it out with a different delimiter.
# -*- coding: utf-8 -*-
import csv
from tempfile import NamedTemporaryFile
import shutil
readfile = "SOME\\PATH\\input.csv"
writefile = "SOME\\PATH\\output.csv"
tempfile = NamedTemporaryFile(delete=False)
with open(readfile, 'rb') as csvfilein, tempfile:
reader = csv.reader(csvfilein, delimiter='|', quotechar='"')
writer = csv.writer(tempfile, delimiter=';', quotechar='"')
for row in reader:
writer.writerow(row)
shutil.move(tempfile.name, writefile)
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment