3

So I have a number of text files with a line like this:

STRT .M                 -9.0:  START DEPTH

I wish to detect the negative number and replace it with 0.1.

I can detect the negative number, simply by looking for the '-'

text.count('-')

if text.count('-') > 0, there is a negative number.

My question is: How do I replace '-9.0' in the string that with the number 0.1? Ultimately, I want to output:

STRT .M                  0.1:  START DEPTH
5
  • Note: the negative number varies between files. It's not always -9.0. Commented May 26, 2011 at 0:59
  • Is this fixed-width? (i.e., does it matter if the replacement doesn't have as many characters as the original?) Commented May 26, 2011 at 1:00
  • No, it could be -9.0, -10.1, -11.123, etc. so I need a general solution for replacing any negative decimal number with 0.1. Commented May 26, 2011 at 1:04
  • Let me clarify: if I replace a number like "-100000" with "0.1" and don't add any extra spaces, does it matter that the rest of the line won't be in the same place as before? Commented May 26, 2011 at 1:06
  • @Tim Yates I don't think it would matter, but I am not 100% sure. Commented May 26, 2011 at 1:42

3 Answers 3

6

The simple solution is to user .replace('-9.0','0.1') (see documentation for .replace()), but I think you need more flexible solution based on regular expressions:

import re
new_string = re.sub(r'-\d+\.\d+', '0.1', your_string)
Sign up to request clarification or add additional context in comments.

5 Comments

Agreed. I should have been more clear. The negative number could be any negative number.
@Flux Can it be also integer?
Good question. I doubt it would be an integer, but I cannot be 100% sure since I have not looked through each of the ~200 files.
@bradley.ayers You are right - I posted it before I tested it. Now it replaces every negative float with decimal part consisting at least of 1 digit (does not match eg. .5).
Shouldn't the regex include a '-' in front of the decimals?!
4

Looks like you are working with LAS files. You can check out libLAS to see if it works for you. And here is a tutorial.

Comments

1

Regular expressions can do this:

>>> import re
>>> regex = re.compile(r' -\d+(\.\d+)?:')
>>> regex.sub(' 0.1:', 'STRT .M                 -9.0:  START DEPTH')
'STRT .M                 0.1:  START DEPTH'
>>> regex.sub(' 0.1:', 'STRT .M                 -19.01:  START DEPTH')
'STRT .M               0.1:  START DEPTH'
>>> regex.sub(' 0.1:', 'STRT .M                 -9:  START DEPTH')
'STRT .M                 0.1:  START DEPTH'

re.sub documentation

3 Comments

Will this work if the negative number is ANY negative number and not just -9.0?
Note that this wouldn't work for, e.g., -10.0, much less -9 (no decimal place). The fix is simple if it isn't fixed-width, but the OP hasn't specified...
It has been revised and now seems to be able to do just that.

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.