Python Forum
Extract continuous numeric characters from a string in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Extract continuous numeric characters from a string in Python (/thread-32027.html)



Extract continuous numeric characters from a string in Python - Robotguy - Jan-15-2021

I am interested in extracting a number that appears after a set of characters ('AA='). However, the issue is I: (i) am not aware how long the number is, (ii) don't know what appears right after the number (could be a blank space or ANY character except 0-9, consider that I do not know what these characters could be but they are definitely not 0-9).

Given below are few of many inputs that I can have.

Line 1: 123 NUBA AA=1.2345 $BB=1234.55
Line 2: 123 NUBA MM AA=1.2345678&BB=1234.55
Line 3: 123 NUBA RRNJH AA=1.2#ALPHA
...
The result should be: 1.2345 1.2345678 1.2 for each respective line above.

PS: I know how to use .find and get the starting location of AA= but that is not very helpful for the above two conditions. Also, I understand one way could be to loop through each character after after AA= and break if a blank space or anything except 0-9 is seen, but that is clumsy and takes unnecessary space in my code. I am looking for a more neat way of doing this.


RE: Extract continuous numeric characters from a string in Python - BashBedlam - Jan-15-2021

What you want to do is pickup each character after 'AA=' as long as it's a number or a decimal point. Combine those into a string and then convert it to a float. Here is one way to go about that:

data = ['Line 1: 123 NUBA AA=1.2345 $BB=1234.55',
	'Line 2: 123 NUBA MM AA=1.2345678&BB=1234.55',
	'Line 3: 123 NUBA RRNJH AA=1.2#ALPHA']

ACCEPTIBLE = '123456789.'
aa_numbers = []

for line in data :
	temp_number_string = ''
	marker = line.index ('AA=') + 3
	while line [marker] in ACCEPTIBLE :
		temp_number_string += line [marker]
		marker += 1
	aa_numbers.append (float (temp_number_string))

print (aa_numbers)



RE: Extract continuous numeric characters from a string in Python - snippsat - Jan-16-2021

I would usually think of regex with that description,nice way not using regex bye BashBedlam.
So something like this with a combo with compile/finditer make it faster if iterate over large amount of data.
import re

data = '''\
Line 1: 123 NUBA AA=1.2345 $BB=1234.55
Line 2: 123 NUBA MM AA=1.2345678&BB=1234.55
Line 3: 123 NUBA RRNJH AA=1.2#ALPHA'''

pattern =  re.compile(r"AA=([+-]?([0-9]*[.])?[0-9]+)")
for match in pattern.finditer(data):
    print(float(match.group(1)))

Output:
1.2345 1.2345678 1.2