Batch tsv to csv conversion

I recently had to convert tables from tsv to csv format and found several ways to do it in this thread from StackOverflow, including the tsv2csv.py Python script below:

import sys
import csv

tabin = csv.reader(sys.stdin, dialect=csv.excel_tab)
commaout = csv.writer(sys.stdout, dialect=csv.excel)
for row in tabin:
    commaout.writerow(row)

Here is a simple wrapper Bash script to run the conversion in batch:

for file in *.tsv
do
    python tsv2csv.py < $file > ${file%.*}.csv
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s