Mercurial > cpython-lru_cache-weakref
annotate Lib/csv.py @ 47787:b32101c19bf8 legacy-trunk
Grammar fixes
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Fri, 20 Jun 2008 23:14:32 +0000 |
| parents | 85fbd1d265a2 |
| children | 47967690b585 |
| rev | line source |
|---|---|
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
2 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
3 csv.py - read/write/investigate CSV files |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
4 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
5 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
6 import re |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
7 from _csv import Error, __version__, writer, reader, register_dialect, \ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
8 unregister_dialect, get_dialect, list_dialects, \ |
|
34406
b93845ce4454
Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34396
diff
changeset
|
9 field_size_limit, \ |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
10 QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
11 __doc__ |
|
34393
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
12 from _csv import Dialect as _Dialect |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
13 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
14 try: |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
15 from cStringIO import StringIO |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
16 except ImportError: |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
17 from StringIO import StringIO |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
18 |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
19 __all__ = [ "QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE", |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
20 "Error", "Dialect", "excel", "excel_tab", "reader", "writer", |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
21 "register_dialect", "get_dialect", "list_dialects", "Sniffer", |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
22 "unregister_dialect", "__version__", "DictReader", "DictWriter" ] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
23 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
24 class Dialect: |
|
34325
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
25 """Describe an Excel dialect. |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
26 |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
27 This must be subclassed (see csv.excel). Valid attributes are: |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
28 delimiter, quotechar, escapechar, doublequote, skipinitialspace, |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
29 lineterminator, quoting. |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
30 |
|
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
31 """ |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
32 _name = "" |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
33 _valid = False |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
34 # placeholders |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
35 delimiter = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
36 quotechar = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
37 escapechar = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
38 doublequote = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
39 skipinitialspace = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
40 lineterminator = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
41 quoting = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
42 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
43 def __init__(self): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
44 if self.__class__ != Dialect: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
45 self._valid = True |
|
34393
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
46 self._validate() |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
47 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
48 def _validate(self): |
|
34393
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
49 try: |
|
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
50 _Dialect(self) |
|
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
51 except TypeError, e: |
|
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
52 # We do this for compatibility with py2.3 |
|
b6ec1b3b9fce
Replace python-coded validation of csv dialect with a call to the C
Andrew McNamara <andrewm@object-craft.com.au>
parents:
34340
diff
changeset
|
53 raise Error(str(e)) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
54 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
55 class excel(Dialect): |
|
34325
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
56 """Describe the usual properties of Excel-generated CSV files.""" |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
57 delimiter = ',' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
58 quotechar = '"' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
59 doublequote = True |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
60 skipinitialspace = False |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
61 lineterminator = '\r\n' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
62 quoting = QUOTE_MINIMAL |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
63 register_dialect("excel", excel) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
64 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
65 class excel_tab(excel): |
|
34325
ee808e08dd6f
add a couple missing docstrings
Skip Montanaro <skip@pobox.com>
parents:
30632
diff
changeset
|
66 """Describe the usual properties of Excel-generated TAB-delimited files.""" |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
67 delimiter = '\t' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
68 register_dialect("excel-tab", excel_tab) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
69 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
70 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
71 class DictReader: |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
72 def __init__(self, f, fieldnames=None, restkey=None, restval=None, |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
30292
diff
changeset
|
73 dialect="excel", *args, **kwds): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
74 self.fieldnames = fieldnames # list of keys for the dict |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
75 self.restkey = restkey # key to catch long rows |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
76 self.restval = restval # default value for short rows |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
30292
diff
changeset
|
77 self.reader = reader(f, dialect, *args, **kwds) |
|
45981
85fbd1d265a2
Issue #2432: give DictReader the dialect and line_num attributes
Georg Brandl <georg@python.org>
parents:
41145
diff
changeset
|
78 self.dialect = dialect |
|
85fbd1d265a2
Issue #2432: give DictReader the dialect and line_num attributes
Georg Brandl <georg@python.org>
parents:
41145
diff
changeset
|
79 self.line_num = 0 |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
80 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
81 def __iter__(self): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
82 return self |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
83 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
84 def next(self): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
85 row = self.reader.next() |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
86 if self.fieldnames is None: |
|
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
87 self.fieldnames = row |
|
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
88 row = self.reader.next() |
|
45981
85fbd1d265a2
Issue #2432: give DictReader the dialect and line_num attributes
Georg Brandl <georg@python.org>
parents:
41145
diff
changeset
|
89 self.line_num = self.reader.line_num |
|
30632
f9dfdcb897bb
Make the fieldnames argument optional in the DictReader. If self.fieldnames
Skip Montanaro <skip@pobox.com>
parents:
30356
diff
changeset
|
90 |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
91 # unlike the basic reader, we prefer not to return blanks, |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
92 # because we will typically wind up with a dict full of None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
93 # values |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
94 while row == []: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
95 row = self.reader.next() |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
96 d = dict(zip(self.fieldnames, row)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
97 lf = len(self.fieldnames) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
98 lr = len(row) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
99 if lf < lr: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
100 d[self.restkey] = row[lf:] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
101 elif lf > lr: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
102 for key in self.fieldnames[lr:]: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
103 d[key] = self.restval |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
104 return d |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
105 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
106 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
107 class DictWriter: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
108 def __init__(self, f, fieldnames, restval="", extrasaction="raise", |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
30292
diff
changeset
|
109 dialect="excel", *args, **kwds): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
110 self.fieldnames = fieldnames # list of keys for the dict |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
111 self.restval = restval # for writing short dicts |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
112 if extrasaction.lower() not in ("raise", "ignore"): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
113 raise ValueError, \ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
114 ("extrasaction (%s) must be 'raise' or 'ignore'" % |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
115 extrasaction) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
116 self.extrasaction = extrasaction |
|
30356
7504812fdb7e
**kwds arg was missing from __init__ for Dict{Reader,Writer} classes.
Skip Montanaro <skip@pobox.com>
parents:
30292
diff
changeset
|
117 self.writer = writer(f, dialect, *args, **kwds) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
118 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
119 def _dict_to_list(self, rowdict): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
120 if self.extrasaction == "raise": |
|
41145
bbaf6c935663
Patch #1635454: the csv.DictWriter class now includes the offending
Georg Brandl <georg@python.org>
parents:
36157
diff
changeset
|
121 wrong_fields = [k for k in rowdict if k not in self.fieldnames] |
|
bbaf6c935663
Patch #1635454: the csv.DictWriter class now includes the offending
Georg Brandl <georg@python.org>
parents:
36157
diff
changeset
|
122 if wrong_fields: |
|
bbaf6c935663
Patch #1635454: the csv.DictWriter class now includes the offending
Georg Brandl <georg@python.org>
parents:
36157
diff
changeset
|
123 raise ValueError("dict contains fields not in fieldnames: " + |
|
bbaf6c935663
Patch #1635454: the csv.DictWriter class now includes the offending
Georg Brandl <georg@python.org>
parents:
36157
diff
changeset
|
124 ", ".join(wrong_fields)) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
125 return [rowdict.get(key, self.restval) for key in self.fieldnames] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
126 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
127 def writerow(self, rowdict): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
128 return self.writer.writerow(self._dict_to_list(rowdict)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
129 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
130 def writerows(self, rowdicts): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
131 rows = [] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
132 for rowdict in rowdicts: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
133 rows.append(self._dict_to_list(rowdict)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
134 return self.writer.writerows(rows) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
135 |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
136 # Guard Sniffer's type checking against builds that exclude complex() |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
137 try: |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
138 complex |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
139 except NameError: |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
140 complex = float |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
141 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
142 class Sniffer: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
143 ''' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
144 "Sniffs" the format of a CSV file (i.e. delimiter, quotechar) |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
145 Returns a Dialect object. |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
146 ''' |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
147 def __init__(self): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
148 # in case there is more than one possible delimiter |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
149 self.preferred = [',', '\t', ';', ' ', ':'] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
150 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
151 |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
152 def sniff(self, sample, delimiters=None): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
153 """ |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
154 Returns a dialect (or None) corresponding to the sample |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
155 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
156 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
157 quotechar, delimiter, skipinitialspace = \ |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
158 self._guess_quote_and_delimiter(sample, delimiters) |
|
36157
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
159 if not delimiter: |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
160 delimiter, skipinitialspace = self._guess_delimiter(sample, |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
161 delimiters) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
162 |
|
36157
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
163 if not delimiter: |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
164 raise Error, "Could not determine delimiter" |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
165 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
166 class dialect(Dialect): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
167 _name = "sniffed" |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
168 lineterminator = '\r\n' |
|
28825
2fad548391de
Attempt to deal with some obvious errors in the code. These were all
Fred Drake <fdrake@acm.org>
parents:
28804
diff
changeset
|
169 quoting = QUOTE_MINIMAL |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
170 # escapechar = '' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
171 doublequote = False |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
172 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
173 dialect.delimiter = delimiter |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
174 # _csv.reader won't accept a quotechar of '' |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
175 dialect.quotechar = quotechar or '"' |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
176 dialect.skipinitialspace = skipinitialspace |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
177 |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
178 return dialect |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
179 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
180 |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
181 def _guess_quote_and_delimiter(self, data, delimiters): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
182 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
183 Looks for text enclosed between two identical quotes |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
184 (the probable quotechar) which are preceded and followed |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
185 by the same character (the probable delimiter). |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
186 For example: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
187 ,'some text', |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
188 The quote with the most wins, same with the delimiter. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
189 If there is no quotechar the delimiter can't be determined |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
190 this way. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
191 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
192 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
193 matches = [] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
194 for restr in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?", |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
195 '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?", |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
196 '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?" |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
197 '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space) |
|
30292
79b1a9ec5223
use the long names for re compilation options; this makes it easier to
Fred Drake <fdrake@acm.org>
parents:
29422
diff
changeset
|
198 regexp = re.compile(restr, re.DOTALL | re.MULTILINE) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
199 matches = regexp.findall(data) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
200 if matches: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
201 break |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
202 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
203 if not matches: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
204 return ('', None, 0) # (quotechar, delimiter, skipinitialspace) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
205 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
206 quotes = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
207 delims = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
208 spaces = 0 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
209 for m in matches: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
210 n = regexp.groupindex['quote'] - 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
211 key = m[n] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
212 if key: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
213 quotes[key] = quotes.get(key, 0) + 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
214 try: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
215 n = regexp.groupindex['delim'] - 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
216 key = m[n] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
217 except KeyError: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
218 continue |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
219 if key and (delimiters is None or key in delimiters): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
220 delims[key] = delims.get(key, 0) + 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
221 try: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
222 n = regexp.groupindex['space'] - 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
223 except KeyError: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
224 continue |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
225 if m[n]: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
226 spaces += 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
227 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
228 quotechar = reduce(lambda a, b, quotes = quotes: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
229 (quotes[a] > quotes[b]) and a or b, quotes.keys()) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
230 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
231 if delims: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
232 delim = reduce(lambda a, b, delims = delims: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
233 (delims[a] > delims[b]) and a or b, delims.keys()) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
234 skipinitialspace = delims[delim] == spaces |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
235 if delim == '\n': # most likely a file with a single column |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
236 delim = '' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
237 else: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
238 # there is *no* delimiter, it's a single column of quoted data |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
239 delim = '' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
240 skipinitialspace = 0 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
241 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
242 return (quotechar, delim, skipinitialspace) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
243 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
244 |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
245 def _guess_delimiter(self, data, delimiters): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
246 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
247 The delimiter /should/ occur the same number of times on |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
248 each row. However, due to malformed data, it may not. We don't want |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
249 an all or nothing approach, so we allow for small variations in this |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
250 number. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
251 1) build a table of the frequency of each character on every line. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
252 2) build a table of freqencies of this frequency (meta-frequency?), |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
253 e.g. 'x occurred 5 times in 10 rows, 6 times in 1000 rows, |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
254 7 times in 2 rows' |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
255 3) use the mode of the meta-frequency to determine the /expected/ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
256 frequency for that character |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
257 4) find out how often the character actually meets that goal |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
258 5) the character that best meets its goal is the delimiter |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
259 For performance reasons, the data is evaluated in chunks, so it can |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
260 try and evaluate the smallest portion of the data possible, evaluating |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
261 additional chunks as necessary. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
262 """ |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
263 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
264 data = filter(None, data.split('\n')) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
265 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
266 ascii = [chr(c) for c in range(127)] # 7-bit ASCII |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
267 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
268 # build frequency tables |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
269 chunkLength = min(10, len(data)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
270 iteration = 0 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
271 charFrequency = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
272 modes = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
273 delims = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
274 start, end = 0, min(chunkLength, len(data)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
275 while start < len(data): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
276 iteration += 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
277 for line in data[start:end]: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
278 for char in ascii: |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
279 metaFrequency = charFrequency.get(char, {}) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
280 # must count even if frequency is 0 |
|
36146
22dbf5818b0b
Fix for problem with Sniffer class. If your delimiter is whitespace and the
Skip Montanaro <skip@pobox.com>
parents:
34406
diff
changeset
|
281 freq = line.count(char) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
282 # value is the mode |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
283 metaFrequency[freq] = metaFrequency.get(freq, 0) + 1 |
|
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
284 charFrequency[char] = metaFrequency |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
285 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
286 for char in charFrequency.keys(): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
287 items = charFrequency[char].items() |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
288 if len(items) == 1 and items[0][0] == 0: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
289 continue |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
290 # get the mode of the frequencies |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
291 if len(items) > 1: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
292 modes[char] = reduce(lambda a, b: a[1] > b[1] and a or b, |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
293 items) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
294 # adjust the mode - subtract the sum of all |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
295 # other frequencies |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
296 items.remove(modes[char]) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
297 modes[char] = (modes[char][0], modes[char][1] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
298 - reduce(lambda a, b: (0, a[1] + b[1]), |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
299 items)[1]) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
300 else: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
301 modes[char] = items[0] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
302 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
303 # build a list of possible delimiters |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
304 modeList = modes.items() |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
305 total = float(chunkLength * iteration) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
306 # (rows of consistent data) / (number of rows) = 100% |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
307 consistency = 1.0 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
308 # minimum consistency threshold |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
309 threshold = 0.9 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
310 while len(delims) == 0 and consistency >= threshold: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
311 for k, v in modeList: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
312 if v[0] > 0 and v[1] > 0: |
|
29169
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
313 if ((v[1]/total) >= consistency and |
|
434ef174f478
* Correct Sniffer doc to correspond to the implementation.
Skip Montanaro <skip@pobox.com>
parents:
28828
diff
changeset
|
314 (delimiters is None or k in delimiters)): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
315 delims[k] = v |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
316 consistency -= 0.01 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
317 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
318 if len(delims) == 1: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
319 delim = delims.keys()[0] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
320 skipinitialspace = (data[0].count(delim) == |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
321 data[0].count("%c " % delim)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
322 return (delim, skipinitialspace) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
323 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
324 # analyze another chunkLength lines |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
325 start = end |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
326 end += chunkLength |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
327 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
328 if not delims: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
329 return ('', 0) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
330 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
331 # if there's more than one, fall back to a 'preferred' list |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
332 if len(delims) > 1: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
333 for d in self.preferred: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
334 if d in delims.keys(): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
335 skipinitialspace = (data[0].count(d) == |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
336 data[0].count("%c " % d)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
337 return (d, skipinitialspace) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
338 |
|
36157
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
339 # nothing else indicates a preference, pick the character that |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
340 # dominates(?) |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
341 items = [(v,k) for (k,v) in delims.items()] |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
342 items.sort() |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
343 delim = items[-1][1] |
|
96eb2a5cfbef
Fix a delimiter detection problem in sniffer. Sniffing "a|b|c\r\n" was
Skip Montanaro <skip@pobox.com>
parents:
36146
diff
changeset
|
344 |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
345 skipinitialspace = (data[0].count(delim) == |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
346 data[0].count("%c " % delim)) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
347 return (delim, skipinitialspace) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
348 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
349 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
350 def has_header(self, sample): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
351 # Creates a dictionary of types of data in each column. If any |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
352 # column is of a single type (say, integers), *except* for the first |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
353 # row, then the first row is presumed to be labels. If the type |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
354 # can't be determined, it is assumed to be a string in which case |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
355 # the length of the string is the determining factor: if all of the |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
356 # rows except for the first are the same length, it's a header. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
357 # Finally, a 'vote' is taken at the end for each column, adding or |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
358 # subtracting from the likelihood of the first row being a header. |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
359 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
360 rdr = reader(StringIO(sample), self.sniff(sample)) |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
361 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
362 header = rdr.next() # assume first row is header |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
363 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
364 columns = len(header) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
365 columnTypes = {} |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
366 for i in range(columns): columnTypes[i] = None |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
367 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
368 checked = 0 |
|
28828
b37af21b77be
rework Sniffer api significantly
Skip Montanaro <skip@pobox.com>
parents:
28825
diff
changeset
|
369 for row in rdr: |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
370 # arbitrary number of rows to check, to keep it sane |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
371 if checked > 20: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
372 break |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
373 checked += 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
374 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
375 if len(row) != columns: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
376 continue # skip rows that have irregular number of columns |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
377 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
378 for col in columnTypes.keys(): |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
379 |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
380 for thisType in [int, long, float, complex]: |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
381 try: |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
382 thisType(row[col]) |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
383 break |
|
29420
f87fcd0870bf
The multiple exception catch should be in a tuple.
Raymond Hettinger <python@rcn.com>
parents:
29411
diff
changeset
|
384 except (ValueError, OverflowError): |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
385 pass |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
386 else: |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
387 # fallback to length of string |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
388 thisType = len(row[col]) |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
389 |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
390 # treat longs as ints |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
391 if thisType == long: |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
392 thisType = int |
|
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
393 |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
394 if thisType != columnTypes[col]: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
395 if columnTypes[col] is None: # add new column type |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
396 columnTypes[col] = thisType |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
397 else: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
398 # type is inconsistent, remove column from |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
399 # consideration |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
400 del columnTypes[col] |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
401 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
402 # finally, compare results against first row and "vote" |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
403 # on whether it's a header |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
404 hasHeader = 0 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
405 for col, colType in columnTypes.items(): |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
406 if type(colType) == type(0): # it's a length |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
407 if len(header[col]) != colType: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
408 hasHeader += 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
409 else: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
410 hasHeader -= 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
411 else: # attempt typecast |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
412 try: |
|
29411
bac2c0e7c20a
SF Patch #744104: Remove eval() from csv
Raymond Hettinger <python@rcn.com>
parents:
29169
diff
changeset
|
413 colType(header[col]) |
|
29422
cf8ae3b0dcdc
One more multiple exception catch should be in a tuple.
Raymond Hettinger <python@rcn.com>
parents:
29420
diff
changeset
|
414 except (ValueError, TypeError): |
|
28804
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
415 hasHeader += 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
416 else: |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
417 hasHeader -= 1 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
418 |
|
0c950e3323ee
cvs is going to be a module again
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
419 return hasHeader > 0 |
