delineating by comma where commas inside quotation marks don't count

Travis Brady travis.brady at gmail.com
Thu Oct 25 10:21:43 EDT 2007


On 10/24/07, Dan Bishop <danb_83 at yahoo.com> wrote:
>
> On Oct 24, 8:56 pm, "Junior" <mlon... at verizon.net> wrote:
> > I want to open a text file for reading and delineate it by comma.  I
> also
> > want any data
> > surrounded by quotation marks that has a comma in it, not to count the
> > commas inside the
> > quotation marks
>
> Use the csv module.
>
> --
>

The csv module is definitely the way to go here as it knows how to handle
commas within text qualifiers (double quotes).  Do something like this:
import csv

reader = csv.reader(open('testfile.txt'))

for ct,row in enumerate(reader):
    print "var2=", row[2]
    if ct>2:
        break

The comma separated list parser in pyparsing is also great for this,
particularly when the input gets dirtier:
http://pyparsing.wikispaces.com/space/showimage/commasep.py

-- 
Travis Brady
http://travisbrady.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071025/79d3d3fd/attachment.html>


More information about the Python-list mailing list