Home > Mobile >  Python parsing numbers out of strings
Python parsing numbers out of strings

Time:01-31

I have a variety of strings that contain numbers:

examples:

>=5.0% or <=-6.25% 
>=6.25% or <=-7.813% 
<2.5% and >-3.125
>=2.5% or <=-3.125%
>=5.0% or <=-6.25% 
<5.0% or >-6.25%
<3.125 and >-3.906%

I am trying to figure out if there is a way that I can write a section of code that will give me both numbers, regardless of number of characters. Or if I am going to have to write code to parse every one differently. These are only some of the examples, I do not know exactly what all of my possible inputs are. And I do not seem to have a set character that I can partition on. I am looking for any suggestions or feedback.

CodePudding user response:

One possible approach to extract the numbers from the strings is to use regular expressions. The pattern for the numbers in the strings could be defined as follows:

pattern = r'[<>=] \s*-?\d .\d %?'

You could then use the re module in Python to find all matches in the strings:

import re

strings = [
">=5.0% or <=-6.25%",
">=6.25% or <=-7.813%",
"<2.5% and >-3.125",
">=2.5% or <=-3.125%",
">=5.0% or <=-6.25%",
"<5.0% or >-6.25%",
"<3.125 and >-3.906%",
]

for string in strings:
numbers = re.findall(pattern, string)
print(numbers)

This will return a list of strings that match the pattern in each input string, which you can then parse into numbers as needed.

  • Related