How can I add space in to several numbers?

MRAB google at mrabarnett.plus.com
Mon Mar 2 16:08:04 EST 2009


Gabriel Genellina wrote:
> 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.
> 
It probably doesn't matter what comes after the '-':

     re.sub(r"(\d)-", r"\1 -", line)



More information about the Python-list mailing list