Regular expression

Rhodri James rhodri at wildebst.demon.co.uk
Tue Jul 21 13:59:32 EDT 2009


On Tue, 21 Jul 2009 17:12:53 +0100, Peter Fodrek <peter.fodrek at stuba.sk>  
wrote:

> 21.7.2009 v 17:39, Rhodri James:
>
>> On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek <peter.fodrek at stuba.sk 
>> > wrote:

[snipped for space]

>>> This handles text file  like
>>>
>>> // remark
>>> PL_OFF
>>> PARK
>>> FS
>>> MA  52.8806 ,  18.0914
>>> SS
>>> AD_W
>>> PL_ON
>>> C   52.3955 ,  -16.1511 ,  -90.0000
>>>
>>> It handles file correctly with two exceptions
>>>
>>> 1) omits ',' in the a output
>>
>> You don't do any output, so it's hard to correct that!
>
> for line
>
> C   52.3955 ,  -16.1511 ,  -90.0000
>
> it returns (as I remember)
>
> ('C', ' ', '52.3955', ' ', ' ', ' 16.1511', ' ', ' ','90.0')
>
> I was to get (number and positions of empty strings are not related to  
> me. I can add, remove then as you wish)
>
> ('C', ' ', '52.3955', ' ', ' ,', ' -16.1511', ' ', ' ,','-90.0')

If whitespace isn't an issue and the format really is as simple as
your example, you don't need the full power of a regular expression
here.  split() would do the trick:

     words = self.line.split()

will return what you want for that example.  It will produce different
(but more consistent) results than the regular expression for other lines,
though: for example,

MA  52.8806 , 18.0914

when put through the regular expression (if fixed for commas) would give  
you

('M', 'A', '  ', '52.8806', ' ', ',', ' ', '18.0914')

as against

['MA', '52.8806', ',', '18.0914']

> I use eval call in python to do format conversion, so just parsing is  
> problem

float() might be better than eval(), if you know for sure that you are
expecting floats.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list