Module csv :: Class Sniffer
[hide private]
[frames] | no frames]

_ClassType Sniffer

"Sniffs" the format of a CSV file (i.e. delimiter, quotechar) Returns a Dialect object.

Instance Methods [hide private]
 
__init__(self)
 
sniff(self, sample, delimiters=None)
Returns a dialect (or None) corresponding to the sample
 
_guess_quote_and_delimiter(self, data, delimiters)
Looks for text enclosed between two identical quotes (the probable quotechar) which are preceded and followed by the same character (the probable delimiter).
 
_guess_delimiter(self, data, delimiters)
The delimiter /should/ occur the same number of times on each row.
 
has_header(self, sample)
Method Details [hide private]

_guess_quote_and_delimiter(self, data, delimiters)

 

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.

_guess_delimiter(self, data, delimiters)

 

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.