I have a program that prints out some data with 'p'
s in place of decimal points, and also some other information. I was trying to replace the 'p'
s with '.'
s.
Example output by the program:
out_info = 'value is approximately 34p55'
I would like to change it to:
out_info_updated = 'value is approximately 34.55'
I tried using re.search
to extract out the number and replace the p
with .
, but plugging it back becomes a problem.I could not figure out that pattern to use for re.sub
that would do the job.
Can anyone please help?
Here you go:
import re
out_info = "value is approximately 34p55"
re.sub(r'(\d+)p(\d+)', r'\1.\2', out_info)
The output is:
'value is approximately 34.55'
That says "Look for one or more digits, followed by a p
, followed by one or more digits, then replace all that with the first set of digits, followed by a .
, followed by the second set of digits."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With