How can I add space in to several numbers?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Mar 2 16:23:27 EST 2009
En Mon, 02 Mar 2009 19:08:04 -0200, MRAB <google at mrabarnett.plus.com>
escribió:
> 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)
Yes, that's better.
--
Gabriel Genellina
More information about the Python-list
mailing list