-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2tsv.py
More file actions
executable file
·25 lines (21 loc) · 867 Bytes
/
Copy pathcsv2tsv.py
File metadata and controls
executable file
·25 lines (21 loc) · 867 Bytes
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
#!/usr/bin/env python
import sys
import csv
import errno
csv.field_size_limit(sys.maxsize) # Or else it cannot handle fields longer than 131072
tabin = csv.reader(sys.stdin, dialect=csv.excel)
commaout = csv.writer(sys.stdout, dialect=csv.excel_tab, lineterminator='\n')
try:
for row in tabin:
# HACK: Replace newlines in fields with "|". tsv has no good support for fields containing
# newlines; excel and google sheets convert them to spaces and do *not* quote the field, unlike
# for csv. Python's csv library just treats them as normal characters and thus breaks the
# format. Even if it quoted the newline-containing field, it would be nightmarish to deal with
# those later.
row = [field.replace('\n','|') for field in row]
commaout.writerow(row)
except IOError as e:
if e.errno == errno.EPIPE:
pass
else:
raise