|
1 | 1 | import re
|
2 | 2 | import csv
|
3 | 3 |
|
4 |
| -from mfr.extensions.tabular.exceptions import EmptyTableError, TabularRendererError |
5 | 4 | from mfr.extensions.tabular import utilities
|
| 5 | +from mfr.extensions.tabular.exceptions import EmptyTableError, TabularRendererError |
6 | 6 |
|
7 | 7 |
|
8 | 8 | def csv_stdlib(fp):
|
9 |
| - """Read and convert a csv file to JSON format using the python standard library |
10 |
| - :param fp: File pointer object |
11 |
| - :return: tuple of table headers and data |
12 |
| - """ |
13 |
| - data = fp.read(2048) |
| 9 | + data = fp.seek(2048) |
14 | 10 | fp.seek(0)
|
| 11 | + # set the dialect instead of sniffing for it. |
| 12 | + # sniffing can cause things like spaces or characters to be the delimiter |
| 13 | + dialect = csv.excel |
| 14 | + try: |
| 15 | + _set_dialect_quote_attrs(dialect, data) |
| 16 | + except: |
| 17 | + # if this errors it is not an exception |
| 18 | + pass |
15 | 19 |
|
| 20 | + reader = csv.DictReader(fp, dialect=dialect) |
| 21 | + return parse_stdlib(reader) |
| 22 | + |
| 23 | +def tsv_stdlib(fp): |
| 24 | + data = fp.seek(2048) |
| 25 | + fp.seek(0) |
| 26 | + # set the dialect instead of sniffing for it. |
| 27 | + # sniffing can cause things like spaces or characters to be the delimiter |
| 28 | + dialect = csv.excel_tab |
16 | 29 | try:
|
17 |
| - dialect = csv.Sniffer().sniff(data) |
18 |
| - except csv.Error: |
19 |
| - dialect = csv.excel |
20 |
| - else: |
21 | 30 | _set_dialect_quote_attrs(dialect, data)
|
| 31 | + except: |
| 32 | + # if this errors it is not an exception |
| 33 | + pass |
22 | 34 |
|
23 | 35 | reader = csv.DictReader(fp, dialect=dialect)
|
| 36 | + return parse_stdlib(reader) |
| 37 | + |
| 38 | +def parse_stdlib(reader): |
| 39 | + """Read and convert a csv like file to JSON format using the python standard library |
| 40 | + :param fp: File pointer object |
| 41 | + :return: tuple of table headers and data |
| 42 | + """ |
24 | 43 | columns = []
|
25 | 44 | # update the reader field names to avoid duplicate column names when performing row extraction
|
26 | 45 | for idx, fieldname in enumerate(reader.fieldnames or []):
|
|
0 commit comments