在Python中分开字符串的特定部分

发布于 2025-02-11 12:10:07 字数 279 浏览 1 评论 0 原文

我正在通过SCPI向电源发送信息。我创建了一个GUI来显示其响应。响应之一是一个错误消息,该消息是字符串,它具有字符,数字,逗号和几个单词中的引号标记。 前任: +0,“无错误” 我需要仅提取引号中的内容,因此显示的内容没有错误。 我试图将其拆分,然后将其与引号截断,但是它永远不会像我需要的那样干净,并且无法通过简单地进行_mystring [4:-1]切割字符串,因为某些错误代码正在发送背部是不同的长度。 IE。 -400,“查询错误”,

因此执行_mymstring [4:-1]将显示”,“查询错误”

帮助

I am sending information via SCPI to power supplies. I created a GUI to display its responses. One of the responses comes is an error message that is a string and it has a character, a number, a comma and a couple of words in quotation marks.
Ex:
+0,"No Error"
I need to extract just what is in the quotation marks, so what would be displayed is No Error.
I was trying to split it and then truncate it from the quotation mark, however it is never as clean as I need and I can't cut the string by simply doing _mystring[4:-1] because some of the error codes being sent back are different lengths.
ie.
-400,"Query Error"

So doing _mystring[4:-1] would display ","Query Error"

help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清风夜微凉 2025-02-18 12:10:07

通过内置 re 库的正则表达式用于提取字符串的部分,与给定模式匹配。

模式说明:'\“(。*)\”'

  • 查找引号标记
  • capture 任何数量的字符,直到找到其他引号标记
  • > .findall(findall)( )方法返回 all 匹配,(零,一个或多个) [0] index,然后简单地返回第一个匹配项。

.findall()方法的文档为

示例代码:

import re

string = '+0,"No Error"'
re.findall('\"(.*)\"', string)[0]

输出:

'No Error'

迭代测试:

str1 = '+0,"No Error"'
str2 = '-400,"Query Error"'
rexp = re.compile('\"(.*)\"')

for string in [str1, str2]:
    print(rexp.findall(string)[0])

输出:

No Error
Query Error

Regular expressions via the built-in re library are used to extract portions of strings, matching a given pattern.

Pattern explanation: '\"(.*)\"'

  • Find a quotation mark
  • Capture any number of characters until another quotation mark is found
  • As the .findall() method returns all matches, (zero, one or more) the [0] index and simply returns the first match.

Documentation for the .findall() method is linked here.

Example code:

import re

string = '+0,"No Error"'
re.findall('\"(.*)\"', string)[0]

Output:

'No Error'

Iterative test:

str1 = '+0,"No Error"'
str2 = '-400,"Query Error"'
rexp = re.compile('\"(.*)\"')

for string in [str1, str2]:
    print(rexp.findall(string)[0])

Output:

No Error
Query Error
千寻… 2025-02-18 12:10:07

使用字符串 split -Method的依从性方法:

response = '+0,"No Error"'

msg = response.split('"')[1]

print(msg)
#No Error

对于更强大的解决方案,请考虑使用正则表达式。

A rudimental approach using string split-method:

response = '+0,"No Error"'

msg = response.split('"')[1]

print(msg)
#No Error

For a more robust solution consider to use a regular expression.

独闯女儿国 2025-02-18 12:10:07

考虑在此处考虑使用正则表达式

import re

message = '-400,"Query Error"'
pattern = '(.)([0-9]+),"([^"]*)"'
result = re.match(pattern, message)

symbol = result.group(1)
code = int(result.group(2))
text = result.group(3)

print(f'symbol: {symbol}\ncode: {code}\ntext: {text}')

模式定义了括号指示的3组。

  1. 第一组包含一个字符,如匹配任何字符。
  2. 第二组包含一个或多个数字的序列
  3. 第三组包含引号中的引号以外的任何数量的字符。引号被排除在小组之外。

Consider using regular expressions

import re

message = '-400,"Query Error"'
pattern = '(.)([0-9]+),"([^"]*)"'
result = re.match(pattern, message)

symbol = result.group(1)
code = int(result.group(2))
text = result.group(3)

print(f'symbol: {symbol}\ncode: {code}\ntext: {text}')

Here pattern defines 3 groups indicated by brackets.

  1. First group contains a single character, as . matches any character.
  2. Second group contains a sequence of one or more digits
  3. Third group contains any number of characters other than a quotation mark in quotes. Quotes are excluded from the group.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文