Last active
February 4, 2016 16:48
-
-
Save sepehr/15e76d1fad820a4619a7 to your computer and use it in GitHub Desktop.
Python: Bulk convert .tsv to .csv
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
#!/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