在Python中查找字符串中变化的子字符串

发布于 2025-01-09 04:28:16 字数 1479 浏览 0 评论 0原文

我正在寻找一种从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒带 2025-01-16 04:28:16

使用切片方法获取最后两位数字,如下所示:

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

输出:

['3.19', '3.01']

或使用基于正则表达式的方法从多行字符串中提取所有点,如下所示:

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)

输出:

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

结果是元组列表(两个坐标的元组)

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)

断爱 2025-01-16 04:28:16

您可以使用正则表达式:(-?[\d.]+) 将仅匹配带有可选“-”的数字。然后只获取最后 2 个数字。

示例:

import re
string = "CRISTART 101 STATUS MODE joint POSJOINTCURRENT -5.26 -4.25"
print(re.findall('(-?[\d.]+)', string)[-2:])

输出:

['5.26', '-4.25']

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']
对你再特殊 2025-01-16 04:28:16

以更基本的形式

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文