Skip to content

Instantly share code, notes, and snippets.

@sepehr
Last active February 4, 2016 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sepehr/15e76d1fad820a4619a7 to your computer and use it in GitHub Desktop.
Save sepehr/15e76d1fad820a4619a7 to your computer and use it in GitHub Desktop.
Python: Bulk convert .tsv to .csv
#!/usr/bin/python
import glob
import csv
import sys
import os
# Check args
if len(sys.argv) < 2:
sys.exit('Usage: tsv2csv.py /path/to/dir')
# Create glob pattern, fetch filenames
pattern = sys.argv[1].rstrip('/') + '/*.tsv'
filenames = glob.glob(pattern)
# Loop through, and convert
for i, source_file in enumerate(filenames):
dest_file = source_file.rstrip('.tsv') + '.csv'
csv.field_size_limit(sys.maxsize)
csv.writer(file(dest_file, 'w+')).writerows(csv.reader(open(source_file), delimiter="\t"))
i += 1
print 'Created: ' + os.path.basename(dest_file)
print '\n', i, 'files have been converted...\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment