Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Created April 6, 2017 16:39
Show Gist options
  • Select an option

  • Save mattmc3/6eb61daf4d0eb04f3ca1332365cfe783 to your computer and use it in GitHub Desktop.

Select an option

Save mattmc3/6eb61daf4d0eb04f3ca1332365cfe783 to your computer and use it in GitHub Desktop.
Python - csv to tsv
#!/usr/bin/env python
"""
CSV to TSV
"""
import csv
import re
def csv2tsv(csvpath, delimiter=',', quotechar='"', newdelimiter='\t'):
whitespacescrub = re.compile('\r|\n|\t')
with open(csvpath) as csvfile:
rdr = csv.reader(csvfile, delimiter=delimiter, quotechar=quotechar)
for row in rdr:
scrubbed_row = [whitespacescrub.sub(" ", x) for x in row]
result = newdelimiter.join(scrubbed_row)
print(result)
if __name__ == '__main__':
csv2tsv("test.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment