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

  • You should look into regular expressions https://www.w3schools.com/python/python_regex.asp – C_Z_ Jun 28 '22 at 18:49
  • 1
    You should include your attempts at splitting. Did you try splitting on comma then splitting on `"`? – wwii Jun 28 '22 at 18:51
  • Have you checked the docs for the [str.split()](https://docs.python.org/3.3/library/stdtypes.html?highlight=split#str.split) method? – G. Anderson Jun 28 '22 at 18:52

3 Answers3

1

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
S3DEV
  • 8,768
  • 3
  • 31
  • 42
0

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.

cards
  • 3,936
  • 1
  • 7
  • 25
  • 1
    This is perfect, I was trying to mess with split earlier but wasn't getting the results I wanted, I understand what I was missing now – Joshua Reck Jun 28 '22 at 20:09
  • @Joshua Reck since the string contains two quotes the split generates three terms and you need the one in the middle – cards Jun 28 '22 at 20:18
0

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.
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76