Problem in splitting a string

Paul McGuire ptmcg at austin.rr._bogus_.com
Thu Jul 22 15:49:20 EDT 2004


"Angelo Secchi" <secchi at sssup.it> wrote in message
news:mailman.719.1090519759.5135.python-list at python.org...
>
> Hi,
> I have string of numbers and words like
>
> ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
>
> and I would like to split (I'm using string.split()) it using comma as
> separator but I do not want to split in two also the "name,surname"
> field. In other word I would like python in separating fields to skip
> that particular comma.
>
> How can I do that?
>
> Thanks in advance
>
> Angelo
>
>
>
> --
> ========================================================
>  Angelo Secchi                     PGP Key ID:EA280337
> ========================================================
>   Current Position:
>   Graduate Fellow Scuola Superiore S.Anna
>   Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
>   ph.: +39 050 883365
>   email: secchi at sssup.it www.sssup.it/~secchi/
> ========================================================

Using pyparsing's commaSeparatedList (designed specifically to handle this
case):

from pyparsing import commaSeparatedList
testdata = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
print commaSeparatedList.parseString( testdata )

Gives:
['', '', '', '', '', '', '23', '', '', 'asd', '', '', '', '',
'"name,surname"', '', '', '', '', '', '', '']

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul





More information about the Python-list mailing list