3 csv.py - read/write/investigate CSV files
7 from _csv
import Error
, __version__
, writer
, reader
, register_dialect
, \
8 unregister_dialect
, get_dialect
, list_dialects
, \
10 QUOTE_MINIMAL
, QUOTE_ALL
, QUOTE_NONNUMERIC
, QUOTE_NONE
, \
12 from _csv
import Dialect
as _Dialect
15 from cStringIO
import StringIO
17 from StringIO
import StringIO
19 __all__
= [ "QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
20 "Error", "Dialect", "excel", "excel_tab", "reader", "writer",
21 "register_dialect", "get_dialect", "list_dialects", "Sniffer",
22 "unregister_dialect", "__version__", "DictReader", "DictWriter" ]
25 """Describe an Excel dialect.
27 This must be subclassed (see csv.excel). Valid attributes are:
28 delimiter, quotechar, escapechar, doublequote, skipinitialspace,
29 lineterminator, quoting.
39 skipinitialspace
= None
44 if self
.__class
__ != Dialect
:
52 # We do this for compatibility with py2.3
56 """Describe the usual properties of Excel-generated CSV files."""
60 skipinitialspace
= False
61 lineterminator
= '\r\n'
62 quoting
= QUOTE_MINIMAL
63 register_dialect("excel", excel
)
65 class excel_tab(excel
):
66 """Describe the usual properties of Excel-generated TAB-delimited files."""
68 register_dialect("excel-tab", excel_tab
)
72 def __init__(self
, f
, fieldnames
=None, restkey
=None, restval
=None,
73 dialect
="excel", *args
, **kwds
):
74 self
.fieldnames
= fieldnames
# list of keys for the dict
75 self
.restkey
= restkey
# key to catch long rows
76 self
.restval
= restval
# default value for short rows
77 self
.reader
= reader(f
, dialect
, *args
, **kwds
)
78 self
.dialect
= dialect
85 row
= self
.reader
.next()
86 if self
.fieldnames
is None:
88 row
= self
.reader
.next()
89 self
.line_num
= self
.reader
.line_num
91 # unlike the basic reader, we prefer not to return blanks,
92 # because we will typically wind up with a dict full of None
95 row
= self
.reader
.next()
96 d
= dict(zip(self
.fieldnames
, row
))
97 lf
= len(self
.fieldnames
)
100 d
[self
.restkey
] = row
[lf
:]
102 for key
in self
.fieldnames
[lr
:]:
103 d
[key
] = self
.restval
108 def __init__(self
, f
, fieldnames
, restval
="", extrasaction
="raise",
109 dialect
="excel", *args
, **kwds
):
110 self
.fieldnames
= fieldnames
# list of keys for the dict
111 self
.restval
= restval
# for writing short dicts
112 if extrasaction
.lower() not in ("raise", "ignore"):
114 ("extrasaction (%s) must be 'raise' or 'ignore'" %
116 self
.extrasaction
= extrasaction
117 self
.writer
= writer(f
, dialect
, *args
, **kwds
)
119 def _dict_to_list(self
, rowdict
):
120 if self
.extrasaction
== "raise":
121 wrong_fields
= [k
for k
in rowdict
if k
not in self
.fieldnames
]
123 raise ValueError("dict contains fields not in fieldnames: " +
124 ", ".join(wrong_fields
))
125 return [rowdict
.get(key
, self
.restval
) for key
in self
.fieldnames
]
127 def writerow(self
, rowdict
):
128 return self
.writer
.writerow(self
._dict
_to
_list
(rowdict
))
130 def writerows(self
, rowdicts
):
132 for rowdict
in rowdicts
:
133 rows
.append(self
._dict
_to
_list
(rowdict
))
134 return self
.writer
.writerows(rows
)
136 # Guard Sniffer's type checking against builds that exclude complex()
144 "Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
145 Returns a Dialect object.
148 # in case there is more than one possible delimiter
149 self
.preferred
= [',', '\t', ';', ' ', ':']
152 def sniff(self
, sample
, delimiters
=None):
154 Returns a dialect (or None) corresponding to the sample
157 quotechar
, delimiter
, skipinitialspace
= \
158 self
._guess
_quote
_and
_delimiter
(sample
, delimiters
)
160 delimiter
, skipinitialspace
= self
._guess
_delimiter
(sample
,
164 raise Error
, "Could not determine delimiter"
166 class dialect(Dialect
):
168 lineterminator
= '\r\n'
169 quoting
= QUOTE_MINIMAL
173 dialect
.delimiter
= delimiter
174 # _csv.reader won't accept a quotechar of ''
175 dialect
.quotechar
= quotechar
or '"'
176 dialect
.skipinitialspace
= skipinitialspace
181 def _guess_quote_and_delimiter(self
, data
, delimiters
):
183 Looks for text enclosed between two identical quotes
184 (the probable quotechar) which are preceded and followed
185 by the same character (the probable delimiter).
188 The quote with the most wins, same with the delimiter.
189 If there is no quotechar the delimiter can't be determined
194 for restr
in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
195 '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?",
196 '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?"
197 '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space)
198 regexp
= re
.compile(restr
, re
.DOTALL | re
.MULTILINE
)
199 matches
= regexp
.findall(data
)
204 return ('', None, 0) # (quotechar, delimiter, skipinitialspace)
210 n
= regexp
.groupindex
['quote'] - 1
213 quotes
[key
] = quotes
.get(key
, 0) + 1
215 n
= regexp
.groupindex
['delim'] - 1
219 if key
and (delimiters
is None or key
in delimiters
):
220 delims
[key
] = delims
.get(key
, 0) + 1
222 n
= regexp
.groupindex
['space'] - 1
228 quotechar
= reduce(lambda a
, b
, quotes
= quotes
:
229 (quotes
[a
] > quotes
[b
]) and a
or b
, quotes
.keys())
232 delim
= reduce(lambda a
, b
, delims
= delims
:
233 (delims
[a
] > delims
[b
]) and a
or b
, delims
.keys())
234 skipinitialspace
= delims
[delim
] == spaces
235 if delim
== '\n': # most likely a file with a single column
238 # there is *no* delimiter, it's a single column of quoted data
242 return (quotechar
, delim
, skipinitialspace
)
245 def _guess_delimiter(self
, data
, delimiters
):
247 The delimiter /should/ occur the same number of times on
248 each row. However, due to malformed data, it may not. We don't want
249 an all or nothing approach, so we allow for small variations in this
251 1) build a table of the frequency of each character on every line.
252 2) build a table of freqencies of this frequency (meta-frequency?),
253 e.g. 'x occurred 5 times in 10 rows, 6 times in 1000 rows,
255 3) use the mode of the meta-frequency to determine the /expected/
256 frequency for that character
257 4) find out how often the character actually meets that goal
258 5) the character that best meets its goal is the delimiter
259 For performance reasons, the data is evaluated in chunks, so it can
260 try and evaluate the smallest portion of the data possible, evaluating
261 additional chunks as necessary.
264 data
= filter(None, data
.split('\n'))
266 ascii
= [chr(c
) for c
in range(127)] # 7-bit ASCII
268 # build frequency tables
269 chunkLength
= min(10, len(data
))
274 start
, end
= 0, min(chunkLength
, len(data
))
275 while start
< len(data
):
277 for line
in data
[start
:end
]:
279 metaFrequency
= charFrequency
.get(char
, {})
280 # must count even if frequency is 0
281 freq
= line
.count(char
)
283 metaFrequency
[freq
] = metaFrequency
.get(freq
, 0) + 1
284 charFrequency
[char
] = metaFrequency
286 for char
in charFrequency
.keys():
287 items
= charFrequency
[char
].items()
288 if len(items
) == 1 and items
[0][0] == 0:
290 # get the mode of the frequencies
292 modes
[char
] = reduce(lambda a
, b
: a
[1] > b
[1] and a
or b
,
294 # adjust the mode - subtract the sum of all
296 items
.remove(modes
[char
])
297 modes
[char
] = (modes
[char
][0], modes
[char
][1]
298 - reduce(lambda a
, b
: (0, a
[1] + b
[1]),
301 modes
[char
] = items
[0]
303 # build a list of possible delimiters
304 modeList
= modes
.items()
305 total
= float(chunkLength
* iteration
)
306 # (rows of consistent data) / (number of rows) = 100%
308 # minimum consistency threshold
310 while len(delims
) == 0 and consistency
>= threshold
:
311 for k
, v
in modeList
:
312 if v
[0] > 0 and v
[1] > 0:
313 if ((v
[1]/total
) >= consistency
and
314 (delimiters
is None or k
in delimiters
)):
319 delim
= delims
.keys()[0]
320 skipinitialspace
= (data
[0].count(delim
) ==
321 data
[0].count("%c " % delim
))
322 return (delim
, skipinitialspace
)
324 # analyze another chunkLength lines
331 # if there's more than one, fall back to a 'preferred' list
333 for d
in self
.preferred
:
334 if d
in delims
.keys():
335 skipinitialspace
= (data
[0].count(d
) ==
336 data
[0].count("%c " % d
))
337 return (d
, skipinitialspace
)
339 # nothing else indicates a preference, pick the character that
341 items
= [(v
,k
) for (k
,v
) in delims
.items()]
345 skipinitialspace
= (data
[0].count(delim
) ==
346 data
[0].count("%c " % delim
))
347 return (delim
, skipinitialspace
)
350 def has_header(self
, sample
):
351 # Creates a dictionary of types of data in each column. If any
352 # column is of a single type (say, integers), *except* for the first
353 # row, then the first row is presumed to be labels. If the type
354 # can't be determined, it is assumed to be a string in which case
355 # the length of the string is the determining factor: if all of the
356 # rows except for the first are the same length, it's a header.
357 # Finally, a 'vote' is taken at the end for each column, adding or
358 # subtracting from the likelihood of the first row being a header.
360 rdr
= reader(StringIO(sample
), self
.sniff(sample
))
362 header
= rdr
.next() # assume first row is header
364 columns
= len(header
)
366 for i
in range(columns
): columnTypes
[i
] = None
370 # arbitrary number of rows to check, to keep it sane
375 if len(row
) != columns
:
376 continue # skip rows that have irregular number of columns
378 for col
in columnTypes
.keys():
380 for thisType
in [int, long, float, complex]:
384 except (ValueError, OverflowError):
387 # fallback to length of string
388 thisType
= len(row
[col
])
390 # treat longs as ints
394 if thisType
!= columnTypes
[col
]:
395 if columnTypes
[col
] is None: # add new column type
396 columnTypes
[col
] = thisType
398 # type is inconsistent, remove column from
402 # finally, compare results against first row and "vote"
403 # on whether it's a header
405 for col
, colType
in columnTypes
.items():
406 if type(colType
) == type(0): # it's a length
407 if len(header
[col
]) != colType
:
411 else: # attempt typecast
414 except (ValueError, TypeError):