-
Notifications
You must be signed in to change notification settings - Fork 16
/
csv2tsv
executable file
·38 lines (32 loc) · 1.09 KB
/
csv2tsv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python2.7
r"""
Input is Excel-style CSV. Either stdin or filename.
(We can handle Mac Excel's \r-delimited csv)
Output is honest-to-goodness tsv: no quoting or any \n\r\t.
"""
#from __future__ import print_function
import csv, sys
from tsvutil import cell_text_clean, warning, UnicodeReader
def clean_row(row):
return [cell_text_clean(x) for x in row]
args = sys.argv[:]
args.pop(0)
if len(args)==1:
reader = csv.reader(open(args[0],'U'))
#reader = csv.reader(codecs.open(args[0],'U','utf-8'))
elif len(args) > 1:
raise Exception("No support for multiple files yet")
# could try to enforce conformity, or meld them together, etc.
elif not sys.stdin.isatty():
reader = csv.reader(sys.stdin)
else:
print(__doc__.strip())
sys.exit(1)
header = reader.next()
print "\t".join(clean_row(header)).encode('utf8')
#print(*clean_row(header), sep="\t")
for row in reader:
if len(row) < len(header):
# warning("Row with %d values is too short; padding with %d blanks" % (len(row),len(header)-len(row)))
row += [''] * (len(header) - len(row))
print "\t".join(clean_row(row))