summaryrefslogtreecommitdiffstats
path: root/Lib/csv/util/sniffer.py
blob: 0fb2e1f6673bda64dcd85f1cefcbd166d8077098 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
dialect = Sniffer().sniff(file('csv/easy.csv'))
print "delimiter", dialect.delimiter
print "quotechar", dialect.quotechar
print "skipinitialspace", dialect.skipinitialspace
"""

from csv import csv
import re

# ------------------------------------------------------------------------------
class Sniffer:
    """
    "Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
    Returns a csv.Dialect object.
    """
    def __init__(self, sample = 16 * 1024):
        # in case there is more than one possible delimiter
        self.preferred = [',', '\t', ';', ' ', ':']

        # amount of data (in bytes) to sample
        self.sample = sample


    def sniff(self, fileobj):
        """
        Takes a file-like object and returns a dialect (or None)
        """

        self.fileobj = fileobj

        data = fileobj.read(self.sample)

        quotechar, delimiter, skipinitialspace = self._guessQuoteAndDelimiter(data)
        if delimiter is None:
            delimiter, skipinitialspace = self._guessDelimiter(data)

        class Dialect(csv.Dialect):
            _name = "sniffed"
            lineterminator = '\r\n'
            quoting = csv.QUOTE_MINIMAL
            # escapechar = ''
            doublequote = False
        Dialect.delimiter = delimiter
        Dialect.quotechar = quotechar
        Dialect.skipinitialspace = skipinitialspace

        self.dialect = Dialect
        return self.dialect


    def hasHeaders(self):
        return self._hasHeaders(self.fileobj, self.dialect)


    def register_dialect(self, name = 'sniffed'):
        csv.register_dialect(name, self.dialect)


    def _guessQuoteAndDelimiter(self, data):
        """
        Looks for text enclosed between two identical quotes
        (the probable quotechar) which are preceded and followed
        by the same character (the probable delimiter).
        For example:
                         ,'some text',
        The quote with the most wins, same with the delimiter.
        If there is no quotechar the delimiter can't be determined
        this way.
        """

        matches = []
        for restr in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
                      '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)',   #  ".*?",
                      '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',  # ,".*?"
                      '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'):                            #  ".*?" (no delim, no space)
            regexp = re.compile(restr, re.S | re.M)
            matches = regexp.findall(data)
            if matches:
                break

        if not matches:
            return ('', None, 0) # (quotechar, delimiter, skipinitialspace)

        quotes = {}
        delims = {}
        spaces = 0
        for m in matches:
            n = regexp.groupindex['quote'] - 1
            key = m[n]
            if key:
                quotes[key] = quotes.get(key, 0) + 1
            try:
                n = regexp.groupindex['delim'] - 1
                key = m[n]
            except KeyError:
                continue
            if key:
                delims[key] = delims.get(key, 0) + 1
            try:
                n = regexp.groupindex['space'] - 1
            except KeyError:
                continue
            if m[n]:
                spaces += 1

        quotechar = reduce(lambda a, b, quotes = quotes:
                           (quotes[a] > quotes[b]) and a or b, quotes.keys())

        if delims:
            delim = reduce(lambda a, b, delims = delims:
                           (delims[a] > delims[b]) and a or b, delims.keys())
            skipinitialspace = delims[delim] == spaces
            if delim == '\n': # most likely a file with a single column
                delim = ''
        else:
            # there is *no* delimiter, it's a single column of quoted data
            delim = ''
            skipinitialspace = 0

        return (quotechar, delim, skipinitialspace)


    def _guessDelimiter(self, data):
        """
        The delimiter /should/ occur the same number of times on
        each row. However, due to malformed data, it may not. We don't want
        an all or nothing approach, so we allow for small variations in this
        number.
          1) build a table of the frequency of each character on every line.
          2) build a table of freqencies of this frequency (meta-frequency?),
             e.g.  "x occurred 5 times in 10 rows, 6 times in 1000 rows,
             7 times in 2 rows"
          3) use the mode of the meta-frequency to determine the /expected/
             frequency for that character
          4) find out how often the character actually meets that goal
          5) the character that best meets its goal is the delimiter
        For performance reasons, the data is evaluated in chunks, so it can
        try and evaluate the smallest portion of the data possible, evaluating
        additional chunks as necessary.
        """

        data = filter(None, data.split('\n'))

        ascii = [chr(c) for c in range(127)] # 7-bit ASCII

        # build frequency tables
        chunkLength = min(10, len(data))
        iteration = 0
        charFrequency = {}
        modes = {}
        delims = {}
        start, end = 0, min(chunkLength, len(data))
        while start < len(data):
            iteration += 1
            for line in data[start:end]:
                for char in ascii:
                    metafrequency = charFrequency.get(char, {})
                    freq = line.strip().count(char) # must count even if frequency is 0
                    metafrequency[freq] = metafrequency.get(freq, 0) + 1 # value is the mode
                    charFrequency[char] = metafrequency

            for char in charFrequency.keys():
                items = charFrequency[char].items()
                if len(items) == 1 and items[0][0] == 0:
                    continue
                # get the mode of the frequencies
                if len(items) > 1:
                    modes[char] = reduce(lambda a, b: a[1] > b[1] and a or b, items)
                    # adjust the mode - subtract the sum of all other frequencies
                    items.remove(modes[char])
                    modes[char] = (modes[char][0], modes[char][1]
                                   - reduce(lambda a, b: (0, a[1] + b[1]), items)[1])
                else:
                    modes[char] = items[0]

            # build a list of possible delimiters
            modeList = modes.items()
            total = float(chunkLength * iteration)
            consistency = 1.0 # (rows of consistent data) / (number of rows) = 100%
            threshold = 0.9  # minimum consistency threshold
            while len(delims) == 0 and consistency >= threshold:
                for k, v in modeList:
                    if v[0] > 0 and v[1] > 0:
                        if (v[1]/total) >= consistency:
                            delims[k] = v
                consistency -= 0.01

            if len(delims) == 1:
                delim = delims.keys()[0]
                skipinitialspace = data[0].count(delim) == data[0].count("%c " % delim)
                return (delim, skipinitialspace)

            # analyze another chunkLength lines
            start = end
            end += chunkLength

        if not delims:
            return ('', 0)

        # if there's more than one, fall back to a 'preferred' list
        if len(delims) > 1:
            for d in self.preferred:
                if d in delims.keys():
                    skipinitialspace = data[0].count(d) == data[0].count("%c " % d)
                    return (d, skipinitialspace)

        # finally, just return the first damn character in the list
        delim = delims.keys()[0]
        skipinitialspace = data[0].count(delim) == data[0].count("%c " % delim)
        return (delim, skipinitialspace)


    def _hasHeaders(self, fileobj, dialect):
        # Creates a dictionary of types of data in each column. If any column
        # is of a single type (say, integers), *except* for the first row, then the first
        # row is presumed to be labels. If the type can't be determined, it is assumed to
        # be a string in which case the length of the string is the determining factor: if
        # all of the rows except for the first are the same length, it's a header.
        # Finally, a 'vote' is taken at the end for each column, adding or subtracting from
        # the likelihood of the first row being a header.

        def seval(item):
            """
            Strips parens from item prior to calling eval in an attempt to make it safer
            """
            return eval(item.replace('(', '').replace(')', ''))

        fileobj.seek(0) # rewind the fileobj - this might not work for some file-like objects...

        reader = csv.reader(fileobj,
                            delimiter = dialect.delimiter,
                            quotechar = dialect.quotechar,
                            skipinitialspace = dialect.skipinitialspace)

        header = reader.next() # assume first row is header

        columns = len(header)
        columnTypes = {}
        for i in range(columns): columnTypes[i] = None

        checked = 0
        for row in reader:
            if checked > 20: # arbitrary number of rows to check, to keep it sane
                break
            checked += 1

            if len(row) != columns:
                continue # skip rows that have irregular number of columns

            for col in columnTypes.keys():
                try:
                    try:
                        # is it a built-in type (besides string)?
                        thisType = type(seval(row[col]))
                    except OverflowError:
                        # a long int?
                        thisType = type(seval(row[col] + 'L'))
                        thisType = type(0) # treat long ints as int
                except:
                    # fallback to length of string
                    thisType = len(row[col])

                if thisType != columnTypes[col]:
                    if columnTypes[col] is None: # add new column type
                        columnTypes[col] = thisType
                    else: # type is inconsistent, remove column from consideration
                        del columnTypes[col]

        # finally, compare results against first row and "vote" on whether it's a header
        hasHeader = 0
        for col, colType in columnTypes.items():
            if type(colType) == type(0): # it's a length
                if len(header[col]) != colType:
                    hasHeader += 1
                else:
                    hasHeader -= 1
            else: # attempt typecast
                try:
                    eval("%s(%s)" % (colType.__name__, header[col]))
                except:
                    hasHeader += 1
                else:
                    hasHeader -= 1

        return hasHeader > 0