Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding a changing sub-string within a string in Python

I looking for a way to extract two substrings from a string in Python.
My string can be one of the following 4 examples:

"CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.19 3.01"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT -2.03 3.04"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.06 -8.57"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT -5.26 -4.25"

For information (and this is important) those strings are actually not as simple as the simplified example provided above (for clarity), in reality one string is more like this:

"CRISTART 111 STATUS MODE joint POSJOINTSETPOINT 3.19 3.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 POSJOINTCURRENT 3.19 3.01 0.00 0.00 0.00 0.00 0.00 
0.00 0.00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 POSCARTROBOT 3.2 3.0 0.0 0.00 -0.00 0.00 POSCARTPLATTFORM 0.0 0.0 0.0 OVERRIDE 15.0 DIN 0 DOUT 0 ESTOP 3 SUPPLY 0 CURRENTALL 0 CURRENTJOINTS 4116 4116 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ERROR _MNE_LAG 20 20 0 0 0 0 0 0 0 4 4 4 4 4 4 4 KINSTATE 0 CRIEND\n"

I am interested in extracting the two values right after "POSJOINTCURRENT", which are "3.19" and "3.01" in the first string example provided above, "-2.03" and "3.04" in the second one, "3.06" and "8.57" in the third string, and "-5.26" and "-4.25" in the last string (note the possibility of negative values preceded by a "-" sign, which is the reason of my current struggle achieving what I want).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To extract these numbers from such string, I am currently using this code:

XStart = myString.find("POSJOINTCURRENT") + 16
XEnd = myString.find("POSJOINTCURRENT") + 20
Xcoord = mystring[XStart:XEnd]

YStart = myString.find("POSJOINTCURRENT") + 21
YEnd = myString.find("POSJOINTCURRENT") + 25
Ycoord = mystring[YStart:YEnd]

However, because of the "-" sign that can occur, my code isn’t working as I wish to correctly extract those numbers when they are negative (because I wish the potential "-" sign, as well as the 3 digits (and the "." in between them) to all be extracted.

Any ideas on how to achieve this are welcome. Thank you in advance for your help

>Solution :

Use slice method to get last two digits, like this:

data = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.19 3.01"
result = data.split()[-2:] #Last two values
print(result)

Output:

['3.19', '3.01']

Or use regex based method to extract all of points from multiline strings like this:

data = '''
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.19 3.01"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT -2.03 3.04"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.06 -8.57"
"CRISTART 101 STATUS MODE joint POSJOINTCURRENT -5.26 -4.25"
'''

import re
result = re.findall(r"(?<=POSJOINTCURRENT )(-?\d+.\d+)\s+(-?\d+.\d+)", data)
print(result)

Output:

[('3.19', '3.01'), ('-2.03', '3.04'), ('3.06', '-8.57'), ('-5.26', '-4.25')]

Result is a list of tuple (tuple of two coordinates)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading