Comma delimited ASCII

Skip Montanaro skip at mojam.com
Mon Aug 28 09:40:22 EDT 2000


    Peter> how about a simple solution:

    >>> line = '"This","is","a","sample","data, set","there", "are", "also",1,2,3,4,"numbers"'
    >>> exec('data = ('+line+')')
    >>> data
    ('This', 'is', 'a', 'sample', 'data, set', 'there', 'are', 'also', 1, 2, 3, 4, 'numbers')

This works fine for the common case,  but will fail when trying to eval
something that contains double quotes in a string, since CSV's quoting
methods are different than Python's:

    >>> line = '"This contains a double quote: """,1,2,"also no quotes"'
    >>> exec('data = ('+line+')')
    >>> data
    ('This contains a double quote: ', 1, 2, 'also no quotes')

A more robust solution might be to use Laurence Tratt's CSV module.  I've
attached my somewhat modified version (which fixes a quote parsing bug I
believe).  The original can be found at

    http://eh.org/~laurie/comp/python/csv/index.html

-- 
Skip Montanaro (skip at mojam.com)
http://www.mojam.com/
http://www.musi-cal.com/

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CSV.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20000828/2559f976/attachment.ksh>


More information about the Python-list mailing list