[Csv] PEP 305

Skip Montanaro skip at pobox.com
Wed Oct 1 18:29:11 CEST 2003


    Roberto> I'm kind of disappointed by the csv module for Python 2.3.  The
    Roberto> following line comes from a .csv files generated by
    Roberto> Stockscreener Deluxe (moneycentral.msn.com)

    Roberto> "INTC.""Intel Corporation"".""1"".""2,07"".""0,22"".""13,00"".""53.669.700"".""28,37"""

    Roberto> the following class:

    Roberto> class deluxe_screener(excel):
    Roberto>     delimiter = '.'
    Roberto>     quotechar = '"'
    Roberto>     doublequote = True

    Roberto> cannot produce as an output anything better than

    Roberto> ['INTC', 'Intel Corporation""', '1""', '2,07""', '0,22""', '13,00""', 
    Roberto> '53', '669', '700""', '28,37""']

    Roberto> I'm disappointed by how the double quotes are dealt with, but
    Roberto> above all by the fact that ""53.669.700"" is split into three
    Roberto> separated tokens.

    Roberto> Am I doing something wrong?

Roberto,

I'm not sure you're doing anything wrong.  The CSV file looks invalid to me,
even considering that you are using a European locale.  Can you send me
(skip at pobox.com) a CSV file as an attachment so we can be sure it's not
mangled during transmission?

Here's why I think it's invalid.  If the quotechar is '"', that means any
time you have a space or the delimiter in a field, the field must be quoted.
Furthermore, if the field contains a literal quotechar, it must be doubled.
Accordingly, as you transmitted that row in your message, I see only a
single field.  The first field is opened by the '"' character.  All the
other '"' characters except the last are doubled, meaning they are part of
the field.  The line is closed with a tripled '"', indicating an embedded
quotation mark followed by a '"' to end the field.

Using the attached CSV file (which I think is correct and uses your screener
object, I get

    ['INTC', 'Intel Corporation', '1', '2,07', '0,22', '13,00', '53', '669', '700', '28,37']

which looks fine to me.

-- 
Skip Montanaro
Got gigs? http://www.musi-cal.com/
          http://www.mojam.com/
Got spam? http://spambayes.sf.net/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: intc.csv
Type: application/octet-stream
Size: 59 bytes
Desc: not available
Url : http://mail.python.org/pipermail/csv/attachments/20031001/01248d04/attachment.obj 


More information about the Csv mailing list