在Python中查找字符串中变化的子字符串
我正在寻找一种从 Python 字符串中提取两个子字符串的方法。 我的字符串可以是以下 4 个示例之一:
"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"
仅供参考(这很重要),这些字符串实际上并不像上面提供的简化示例那么简单(为了清楚起见),实际上一个字符串更像是这样:
"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"
我感兴趣提取“POSJOINTCURRENT”之后的两个值,即上面提供的第一个字符串示例中的“3.19”和“3.01”,第二个字符串示例中的“-2.03”和“3.04”,“3.06”和第三个字符串中的“8.57”,最后一个字符串中的“-5.26”和“-4.25”(请注意前面有“-”符号的负值的可能性,这是我当前努力实现我想要的目标的原因) 。
为了从这样的字符串中提取这些数字,我目前正在使用以下代码:
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]
但是,由于可能出现“-”号,我的代码无法工作,因为我希望在这些数字为负数时正确提取这些数字(因为我希望潜在的“-”号以及 3 个数字(以及它们之间的“.”)都将被提取出来,
欢迎您提前感谢您的帮助。
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用切片方法获取最后两位数字,如下所示:
输出:
或使用基于正则表达式的方法从多行字符串中提取所有点,如下所示:
输出:
结果是元组列表(两个坐标的元组)
Use slice method to get last two digits, like this:
Output:
Or use regex based method to extract all of points from multiline strings like this:
Output:
Result is a list of tuple (tuple of two coordinates)
您可以使用正则表达式:
(-?[\d.]+)
将仅匹配带有可选“-”的数字。然后只获取最后 2 个数字。示例:
输出:
You can use regex:
(-?[\d.]+)
will match only numbers with optional "-". Then get only last 2 numbers.Example:
Output:
以更基本的形式
In a more basic form