How can I add space in to several numbers?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Mar 2 15:15:33 EST 2009


En Mon, 02 Mar 2009 17:58:01 -0200, Bo Zhang <primrosebo33 at gmail.com>  
escribió:

> I want to parse a file and do this :
>
>     A  74.335 -86.474-129.317  1.00 54.12
>
> then add space between -86.474 and -129.317. I can get the file with
>
>     A  74.335 -86.474 -129.317  1.00 54.12

Use a regular expression:

py> import re
py> line = "A  74.335 -86.474-129.317  1.00 54.12"
py> re.sub(r"(\d)-(\d)", r"\1 -\2", line)
'A  74.335 -86.474 -129.317  1.00 54.12'

You many require r"(\d)-([\d\.])" if a number is allowed to start with a  
decimal point.

-- 
Gabriel Genellina




More information about the Python-list mailing list