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).
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
CodePudding user response:
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)
CodePudding user response:
You can use regex: (-?[\d.] )
will match only numbers with optional "-". Then get only last 2 numbers.
Example:
import re
string = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT -5.26 -4.25"
print(re.findall('(-?[\d.] )', string)[-2:])
Output:
['5.26', '-4.25']
CodePudding user response:
In a more basic form
string_1 = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.19 3.01"
string_2 = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT -2.03 3.04"
string_3 = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT 3.06 -8.57"
string_4 = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT -5.26 -4.25"
strings = [string_1, string_2, string_3, string_4]
coords = []
for s in strings:
splits = s.split(' ')
x, y = splits[-2:]
coords.append((x, y))
print(coords)