Re: Problem in splitting a string
From: Jens Thiede (jens.usenet_at_webgear.co.za)
Date: 07/22/04
- Next message: Dave Brueck: "Re: Problem in splitting a string"
- Previous message: wes weston: "Re: Problem in splitting a string"
- In reply to: Angelo Secchi: "Problem in splitting a string"
- Next in thread: Dave Brueck: "Re: Problem in splitting a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Dave Brueck: "Re: Problem in splitting a string"
- Previous message: wes weston: "Re: Problem in splitting a string"
- In reply to: Angelo Secchi: "Problem in splitting a string"
- Next in thread: Dave Brueck: "Re: Problem in splitting a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|