Home > Back-end >  Separating specific portion of a string in python
Separating specific portion of a string in python

Time:06-29

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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.
  • Related