[Csv] Re: [PEP305] Python 2.3: a small change request in CSV module

Skip Montanaro skip at pobox.com
Sun May 18 13:12:05 CEST 2003


    Bernard> Sorry Skip, it's been a while since I used patch:

Cd to the top of your Python source tree and execute

    patch -p 0 < csv.diff

    Bernard> Maybe there already was some heuristic weighting 'likely'
    Bernard> separators in the sniffer, after all? Well, checking the
    Bernard> implementation, there's indeed the Sniffer.preferred list of
    Bernard> separators which sets sensible defaults (including ',' and ';'
    Bernard> with which I was concerned in the 1st place).

There are two _guess functions.  _guess_quote_and_delimiter and
_guess_delimiter.  Here are their doc strings:

        """
        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.
        """

        """
        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.
        """

First the q_and_d version is called.  If that fails the less restrictive one
is called.

    Bernard> So... in the end I think I raised a false alarm, and should have 
    Bernard> checked and tested more after you warned me -fair enough- 
    Bernard> that the sniffer can't always be right.
    Bernard> The new parameter works, but will probably very rarely be needed 
    Bernard> given the reasonable defaults. Someone with a *really* untypical 
    Bernard> input will always be able to explicitly set the delimiter.

It's probably worth having nonetheless, just because we can construct
"reasonable" CSV files on which it guesses wrong.

    Bernard> So it's up to you to decide whether the additional control
    Bernard> level is worth keeping, or just adds to the confusion. What I'd
    Bernard> suggest, though, is that the documentation for the sniffer
    Bernard> should explicily show the set of separators it favors (',',
    Bernard> '\t', ';', ' ', ':').

I'm not sure it favors any delimiters.  I think it depends on frequency and
regularity.  I don't know the delimiter guessing code well and am
disinclined to guess about what it favors.

Skip




More information about the Csv mailing list