Skip to content

Instantly share code, notes, and snippets.

@jj1bdx
Forked from netj/csv2tsv.py
Last active November 18, 2018 21:49
Show Gist options
  • Save jj1bdx/c499114ddcad9b83f498 to your computer and use it in GitHub Desktop.
Save jj1bdx/c499114ddcad9b83f498 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys,csv
for row in csv.reader(sys.stdin):
print "\t".join(row)
python -c "`printf '%s\n' 'import sys,csv' 'for row in csv.reader(sys.stdin):' ' print "\t".join(row)'`"
@gamesguru
Copy link

Use this, e.g.

./csv2tsv.py Nutrient.csv

outputs tsv Nutrient.txt

#!/usr/bin/env python
import sys,csv

file = sys.argv[1]
with open(sys.argv[1]) as f:
    with open(file.replace('.csv', '.txt'), 'w+') as f2:
        for row in csv.reader(f):    
            f2.write('\t'.join(row) + '\n')

@gamesguru
Copy link

even better, it does all .csv files in os.getcwd()

surprisingly quick too.

#!/usr/bin/env python
import sys
import csv
import os

#file = sys.argv[1]
for file in os.listdir(os.getcwd()):
    if file.endswith('.csv'):
        with open(file) as f:
            with open(file.replace('.csv', '.txt'), 'w+') as f2:
                for row in csv.reader(f):
                    f2.write('\t'.join(row) + '\n')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment