[Tutor] parsing a blank-separated string into a list

Eugene Leitl Eugene.Leitl@lrz.uni-muenchen.de
Wed, 6 Jun 2001 15:45:12 +0200 (MET DST)


On Wed, 6 Jun 2001, Simon Brunning wrote:

> Try this
>
> mystring = '''10 11 C 9.13 -8.85 C 10.34 -8.15 C 7.92 -8.15 C 10.34 -6.75 C
> 7.92 -6.75 C 8.43 -3.89 C 9.83 -3.89 C 8.00 -5.23 C 10.26 -5.23 C+ 9.13
> -6.05 1 2 2 1 3 1 2 4 1 3 5 2 4 10 2 5 10 1 6 7 1 6 8 1 7 9 1 8 10 1 9 10
> 1'''
> mylist = mystring.split()

Thanks, but I'm stuck with 1.5.2, as 2.1 wouldn't build correctly (can't
do freezing, and too dumb to get the configuration right, as the build
procedure has changed with 2.1) on the AIX box I'm developing for.

Hence I'll stick to the suggestion of the other poster:

import string
jmelist = string.split(jmevalue)

Which works for 1.5.2. Yay!

> At this point, mylist consists of a list of strings. It looks like you want
> the numeric strings converted into numbers. Floats by the look of it. Now,

Usually a good guess, but not in this case. See below.

> if your strings were *all* numeric, you could do:
>
> mylist = [float(value) for value in mylist]
>
> and you would be fine. List comprehensions are nice. ;-)
>
> But in your case, this would blow up with a ValueError when trying to
> convert the alphabetic characters, so we'll have to do this the old
> fashioned way:
>
> for index in range(len(mylist)):
>     try:
>         mylist[index] = float(mylist[index])
>     except ValueError:
>         pass
>
> BTW, if you are not familiar with floats, you have a lot of surprises coming
> up. If you are, then you *still* have a lot of surprises coming up. ;-)

Oh, I'm familiar with them, I used to do numerics on big boxes, at least
as a user. Any "surprises" will be contained by me not processing the
float information. All I need to do is to rearrange the jme string into a
mol type:

5 6 C 8.76 -3.88 C 8.06 -5.09 C 5.96 -3.88 C 6.66 -2.66 C 7.36 -3.88 1 2 1
1 5 1 2 5 1 3 4 1 3 5 1 4 5 1

to:




  5  6  0  0  0  0  0  0  0  0  1 V2000
    8.7600   -3.8800    0.0000 C   0  0  0  0  0  0
    8.0600   -5.0900    0.0000 C   0  0  0  0  0  0
    5.9600   -3.8800    0.0000 C   0  0  0  0  0  0
    6.6600   -2.6600    0.0000 C   0  0  0  0  0  0
    7.3600   -3.8800    0.0000 C   0  0  0  0  0  0
  1  2  1  0  0  0
  1  5  1  0  0  0
  2  5  1  0  0  0
  3  4  1  0  0  0
  3  5  1  0  0  0
  4  5  1  0  0  0
M  END

It is a stupid format, but I don't have a choice. I'll post the subroutine
when I'm done, could be useful for some folks.

-- Eugene Leitl
______________________________________________________________
ICBMTO  : N48 10'07'' E011 33'53'' http://www.lrz.de/~ui22204
57F9CFD3: ED90 0433 EB74 E4A9 537F CFF5 86E7 629B 57F9 CFD3