-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsv.py
147 lines (138 loc) · 3.67 KB
/
tsv.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from StringIO import StringIO
from itertools import chain, cycle
import codecs
def read_cell(i, text):
r"""
>>> read_cell(0, '''a''')
(1, 'a')
>>> read_cell(0, '''a\t''')
(1, 'a')
>>> read_cell(0, '''a\tb''')
(1, 'a')
>>> read_cell(0, '''a\n''')
(1, 'a')
>>> read_cell(0, '''a\nb\n''')
(1, 'a')
>>> read_cell(0, '''"a"''')
(3, 'a')
>>> read_cell(0, '''"a"\t''')
(3, 'a')
>>> read_cell(0, '''"a"\n''')
(3, 'a')
>>> read_cell(0, '''"("")"''')
(6, '(")')
>>> read_cell(0, '''"\n"''')
(3, '\n')
>>> read_cell(0, '''"(""-"")"''')
(9, '("-")')
>>> read_cell(0, '''"(\n""\n)"''')
(8, '(\n"\n)')
>>> read_cell(0, '''"9,10"''')
(6, '9,10')
"""
if len(text) <= i:
return [i, ""]
elif text[i] == '"':
cell = ""
j = i + 1
while True:
q = text[j:].find('"')
next = j + q + 1
if len(text) > next and text[next] == '"':
cell = cell + text[j:next]
j = next + 1
else:
cell = cell + text[j:j+q]
j = j + q
break
return (j + 1, cell)
else:
n = text[i:].find('\n')
t = text[i:].find('\t')
if n == -1 and t == -1:
end = len(text)
return (end, text[i:end])
elif n == -1 or t == -1:
nt = max(n, t)
return (i + nt, text[i:i+nt])
elif n < t:
return (i + n, text[i:i+n])
else:
return (i + t, text[i:i+t])
def read_row(i, text):
r"""
>>> read_row(0, "")
(0, [''])
>>> read_row(0, "a")
(1, ['a'])
>>> read_row(0, "a\tb")
(3, ['a', 'b'])
>>> read_row(0, '"\n"\t""\n')
(7, ['\n', ''])
>>> read_row(0, '\t"abc"')
(6, ['', 'abc'])
>>> read_row(0, '\t"9,10"')
(7, ['', '9,10'])
"""
row = []
while True:
i, cell = read_cell(i, text)
row.append(cell)
if i >= len(text):
break
elif text[i] == '\n':
i += 1
break
elif text[i] == '\t':
i += 1
else:
assert False, 'invalid cell separator %r' % text[i]
return (i, row)
def read_table(i, text):
r"""
>>> list(read_table(0, ""))
[['']]
>>> list(read_table(0, "\n"))
[['']]
>>> list(read_table(0, "a\nb"))
[['a'], ['b']]
>>> list(read_table(0, "a\nb\n"))
[['a'], ['b']]
>>> list(read_table(0, '"\n"\t"\n"\n'))
[['\n', '\n']]
"""
while True:
i, row = read_row(i, text)
yield row
if i >= len(text):
break
def reader(stream):
if not isinstance(stream, basestring):
stream = stream.read()
return read_table(0, stream)
def DictReader(stream):
r"""
>>> list(DictReader('a\tb\n"1"\t"2"\n'))
[{'a': '1', 'b': '2'}]
>>> list(DictReader('a\tb\n"1"\t"2"\n3\t4\n'))
[{'a': '1', 'b': '2'}, {'a': '3', 'b': '4'}]
"""
rows = reader(stream)
headers = rows.next()
for row in rows:
yield dict(zip(headers, chain(row, cycle([""]))))
def format(table):
return u"".join(
u"%s\n" % line for line in (
u"\t".join(
u"%s" % cell.decode('utf-8')
if isinstance(cell, str)
else u"%s" % cell
for cell in row
)
for row in table
)
)
if __name__ == '__main__':
from doctest import testmod
testmod()