-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2tsv.py
More file actions
35 lines (25 loc) · 824 Bytes
/
csv2tsv.py
File metadata and controls
35 lines (25 loc) · 824 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
26
27
28
29
30
31
32
33
34
35
# Translate CSV files to TSV files
import exceptions, re, sys
from record import batch, write_tsv
field_re = re.compile('"([^"]*)"')
class CsvException(exceptions.Exception): pass
def split_fields(line):
fields = []
tail = line
if tail:
while True:
m = field_re.match(tail)
if not m: raise CsvException, ('Field expected', tail)
field = m.group(1)
fields.append(field)
tail = tail[m.end(0):]
if not tail: break
if tail[0] != ',': raise CsvException, ('Comma or end of line expected', tail)
tail = tail[1:]
## log('Fields', fields)
return fields
data, field_names = batch(sys.argv[1:], split_fields)
if len(sys.argv) > 0:
## print "Argv:", sys.argv
pass
write_tsv(data, field_names)