2

I have strings like this in a number of files:

 STEP.M                                   0.02540:STEP LENGTH

(notice the space in the first character of the string) In some of the files, there could be additional spaces between some characters:

STEP . M                                   0.02540 : STEP LENGTH

How can I replace the decimal number with another decimal number? i.e. I want this:

STEP.M                                   1.50000:STEP LENGTH

Edit to add: Naturally, the decimal number is not always the same in each file.

2 Answers 2

2

Think you mean this,

re.sub(r'\d+\.\d+', '1.50000', s)
Sign up to request clarification or add additional context in comments.

Comments

1

This is probably a good time for regular expressions.

>>> import re
>>> s1 =  'STEP.M                                   0.02540:STEP LENGTH'
>>> s2 = 'STEP . M                                   0.02540 : STEP LENGTH'
>>> re.sub(r'\d+\.\d+', '1.50000', s1)
'STEP.M                                   1.50000:STEP LENGTH'
>>> re.sub(r'\d+\.\d+', '1.50000', s2)
'STEP . M                                   1.50000 : STEP LENGTH'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.