[Tutor] Quick question regarding Parsing a Delimited string

Kent Johnson kent37 at tds.net
Wed Jul 8 19:49:11 CEST 2009


On Wed, Jul 8, 2009 at 12:13 PM, Garry Bettle<garry.bettle at gmail.com> wrote:
> Hi,
>
> I've been programming for over 20 yrs, but only the last few in python
> and then only in dribs and drabs.
>
> I'm having a difficult time parsing a delimited string.
>
> e.g.
>
> 100657641~GBP~ACTIVE~0~1~~true~5.0~1247065352508~:
> 3818854~0~24104.08~4.5~~22.1~false|4.4~241.67~L~1~4.3~936.0~L~2~4.2~210.54~L~3~|4.5~19.16~B~1~4.6~214.27~B~2~4.7~802.13~B~3~:
> 3991404~1~19974.18~4.7~~21.7~false|4.6~133.01~L~1~4.5~124.83~L~2~4.4~319.33~L~3~|4.7~86.61~B~1~4.8~247.9~B~2~4.9~142.0~B~3~:
> 4031423~2~15503.56~6.6~~15.1~false|6.6~53.21~L~1~6.4~19.23~L~2~6.2~53.28~L~3~|6.8~41.23~B~1~7.0~145.04~B~2~7.2~37.23~B~3~
>
> That is just a selection of the full string - and I've broken it up
> for this email.  It's delimited by : and then by ~ and finally, in
> some cases, | (a pipe).
>
> If the string is called m, I thought I could create a list with
> m.split(":").  I would like to then first of all find in this list the
> entry beginning with e.g. 3991404.

A couple of splits and a search will do it:

In [1]: data = """100657641~GBP~ACTIVE~0~1~~true~5.0~1247065352508~:
   ...:     3818854~0~24104.08~4.5~~22.1~false|4.4~241.67~L~1~4.3~936.0~L~2~4.2~210.54~L~3~|4.5~19.16~B~1~4.6~214.27~B~2~4.7~802.1
3~B~3~:
   ...:
3991404~1~19974.18~4.7~~21.7~false|4.6~133.01~L~1~4.5~124.83~L~2~4.4~319.33~L~3~|4.7~86.61~B~1~4.8~247.9~B~2~4.9~1
42.0~B~3~:
   ...:
4031423~2~15503.56~6.6~~15.1~false|6.6~53.21~L~1~6.4~19.23~L~2~6.2~53.28~L~3~|6.8~41.23~B~1~7.0~145.04~B~2~7.2
~37.23~B~3~"""

In [6]: items = [ item.strip().split('~') for item in data.split(':') ]

The strip() is only needed because when I pasted your string the
interpreter introduced white space.

In [7]: for item in items:
   ...:     print item[0]

100657641
3818854
3991404
4031423

In [9]: for item in items:
   ...:     if item[0] == '3991404':
   ...:         print item[3]

4.7

Kent


More information about the Tutor mailing list