Re: Problem in splitting a string

From: Jens Thiede (jens.usenet_at_webgear.co.za)
Date: 07/22/04


Date: Thu, 22 Jul 2004 20:49:35 +0200

Angelo Secchi wrote:

>
> 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@sssup.it www.sssup.it/~secchi/
> ========================================================

You may want to look at regular expressions, but in the example you gave,
you just have to split the string at the quotation marks, then every second
item in the resulting list, was between quotes:

>>> x = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
>>> x.split('"')
[',,,,,,23,,,asd,,,,,', 'name,surname', ',,,,,,,\n']
>>> '"gsdfg"sdfg'
'"gsdfg"sdfg'
>>> z = '"gsdfg"sdfg'
>>> z.split('"')
['', 'gsdfg', 'sdfg']
>>>

Regular expressions:
http://docs.python.org/lib/node109.html

A Regular Expressions Tutorial:
http://www.amk.ca/python/howto/regex/

Hope That Helped,

Jens.

-- 
Jabber ID: jtza7@jabberafrica.org
Time Zone: UTC +2
Location: South Africa


Relevant Pages

  • Re: Problem in splitting a string
    ... Angelo Secchi wrote: ... > I have string of numbers and words like ... In other word I would like python in separating fields to skip ... > that particular comma. ...
    (comp.lang.python)
  • Re: Problem in splitting a string
    ... Angelo Secchi wrote: ... >I have string of numbers and words like ... >that particular comma. ... working on a DSV module that would handle all of this stuff, ...
    (comp.lang.python)
  • Re: Simple Parse Question
    ... the comma including the comma? ... I.e., if the string is "Hi, how are ... If you need more complex parsing, take a look at Regular expressions. ...
    (microsoft.public.dotnet.languages.vb)
  • Needing replacing a backslash by a comma
    ... I just need to know how to find the third backslash in a string like this: ... and then replace it by a comma so that the string can be like: ... I have been trying with regular expressions but unsuccessfully. ...
    (microsoft.public.scripting.vbscript)
  • Re: CDBL Function truncates decimals
    ... Public Function ConvertNumberforSQL (ByVal sValue As String, ... vlDefaultNumber As Double= 0) as Double ... Since the Spanish formatting has a comma, the SQL statement seems to have ...
    (microsoft.public.vb.general.discussion)