Revision 5170
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/csvs.py | ||
---|---|---|
36 | 36 |
else: info.dialect = None # line of '' indicates EOF = empty stream |
37 | 37 |
return info |
38 | 38 |
|
39 |
tsv_encode_map = [ |
|
40 |
('\t', r'\t'), |
|
41 |
('\n', '\\\n'), |
|
42 |
] |
|
39 |
tsv_encode_map = strings.json_encode_map[:] |
|
40 |
tsv_encode_map.append(('\t', r'\t')) |
|
41 |
tsv_decode_map = strings.flip_map(tsv_encode_map) |
|
43 | 42 |
|
44 | 43 |
class TsvReader: |
45 | 44 |
'''Unlike csv.reader, for TSVs, interprets \ as escaping a line ending but |
46 | 45 |
ignores it before everything else (e.g. \N for NULL). |
47 |
Also interprets '\n' as a newline and '\t' as a tab.
|
|
46 |
Also expands tsv_encode_map escapes.
|
|
48 | 47 |
''' |
49 | 48 |
def __init__(self, stream, dialect): |
50 | 49 |
assert is_tsv(dialect) |
... | ... | |
70 | 69 |
record = record.replace('\r', ending_placeholder) |
71 | 70 |
|
72 | 71 |
row = csv.reader(StringIO.StringIO(record), self.dialect).next() |
73 |
return [v.replace(r'\n', '\n').replace(r'\t', '\t') for v in row]
|
|
72 |
return [strings.replace_all(tsv_decode_map, v) for v in row]
|
|
74 | 73 |
|
75 | 74 |
def reader_class(dialect): |
76 | 75 |
if is_tsv(dialect): return TsvReader |
Also available in: Unified diff
csvs.py: tsv_encode_map: Escape \n as \n (instead of as a \ followed by a newline) for clarity. Added escape for \r by using strings.json_encode_map. TsvReader: Decode all escapes in tsv_encode_map.