我正在通过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
发布评论
评论(3)
通过内置
re
库的正则表达式用于提取字符串的部分,与给定模式匹配。模式说明:
'\“(。*)\”'
> .findall(findall)( )
方法返回 all 匹配,(零,一个或多个)[0]
index,然后简单地返回第一个匹配项。.findall()
方法的文档为。示例代码:
输出:
迭代测试:
输出:
Regular expressions via the built-in
re
library are used to extract portions of strings, matching a given pattern.Pattern explanation:
'\"(.*)\"'
.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:
Output:
Iterative test:
Output:
使用字符串
split
-Method的依从性方法:对于更强大的解决方案,请考虑使用正则表达式。
A rudimental approach using string
split
-method:For a more robust solution consider to use a regular expression.
考虑在此处考虑使用正则表达式
模式
定义了括号指示的3组。。
匹配任何字符。Consider using regular expressions
Here
pattern
defines 3 groups indicated by brackets..
matches any character.