1

I have the following string: ['', '+VZWRSRP: 64,6300,-101.70', '', 'OK', '']

Where I try to put everything after the second comma into the variable PCI, everything after the 3rd comma into variable earfcn and everything after the 4th comman into variable RSRP.

As start I wanted to test it with RSRP and the following regex command:

cellinfo = ['', '+VZWRSRP: 64,6300,-101.70', '', 'OK', '']
rsrp = re.search('-(.+?)\'', cellinfo)

But somehow I can't get it working.

What's a good solution to achieve this?

0

1 Answer 1

1

I missed that the question is specified for micropython, I haven't worked with that, this answer works in normal python

import re
input_string = ", '+VZWRSRP: 64,6300,-101.70', '', 'OK', "
m = re.search(',.*?:(.*?),(.*?),(.*?),.*?,', input_string)
PCL = m.group(1)
earfcn = m.group(2)
RSRP = m.group(3)

returns:

  • PCL = 64
  • earfcn = 6300
  • RSRP = -101.70'

If you want the output to consist only out of values that could be translated to integers or floats:

part = ".*?(-*\d+\.*\d*).*?"
m = re.search(',.*?:{},{},{},.*?,'.format(part,part,part), input_string)

Will do the trick.

If your string is '+VZWRSRP: 64,6300,-101.70', use

part = ".*?(-*\d+\.*\d*).*?"
m = re.search('.*?:{},{},{}'.format(part,part,part), input_string)
Sign up to request clarification or add additional context in comments.

10 Comments

It should be like this in the end: PCI = 64, Earfcn = 6300 and RSRP = 101.70. Therefore it's unfortunately not correct what you wrote.
Basically just 64 should be in variable pci, 6300 into variable earfcn and -101.70 into variable rsrp. But those values are all dynamic.
Code looks like this now: part = ".*?(-\d+\.*\d).*?" m = re.search(',.*?:{},{},{},.*?,'.format(part,part,part), lte.send_at_cmd('AT+VZWRSRP').split('\r\n')) pci = int(m.group(1)) earfcn = int(m.group(2)) rsrp = float(m.group(3)) but I get this error: Traceback (most recent call last): File "main.py", line 62, in <module> TypeError: can't convert 'list' object to str implicitly the output of lte.send_at_cmd('AT+VZWRSP') looks like this: ['', '+VZWRSRP: 64,6300,-101.70', '', 'OK', '']
The problem is that you split your string into lists using: lte.send_at_cmd('AT+VZWRSRP').split('\r\n'). What does "lte.send_at_cmd('AT+VZWRSRP')" output?
Thats the output: +VZWRSRP: 64,6300,-103.20
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.