-1

What I want to do is a break on the program once a character has appeared a number of times in a string in Python.

This is the output I get:

b'30,22,37,293,29,26,55,30D\r\n$GPGSV,3,1,11,01,04,250,,03,21,309,24,08,15,200,,10,20,157,*7B\r\n$GPGSV,3,2,11,14,49,062,37,16' b'8,W,001328.00,A,A*70\r\n9,26,55,359,30*75\r\n$GPGSV,3,3,11,27,40,170,38,31,20,022,14,32,42,094,35*4C\r\n$GPGLL,0226.72251,N,0' b'A*2E\r\n$GPGGA,001329.00,A,A*70\r\n$GPRMC,001329.00,A,0226.72248,N,07635.92031,W,0.008,,011217,,,A*64\r\n$GPVTG,,T,,M,0.008,N,' b'2.25,1.08,1.98*0D\r\n$GPG1329.00,0226.72248,N,07635.92031,W,1,08,1.08,1769.1,M,9.8,M,,*4C\r\n$GPGSA,A,3,16,32,26,31,14,22,27'

So I want to stop once the fifth "b'" shows up.

dda
  • 6,030
  • 2
  • 25
  • 34
  • What are you trying to accomplish by parsing what appear to be byte strings in this manner? This is almost certainly an XY Problem. – TemporalWolf Dec 01 '17 at 00:26
  • 1
    What have you tried so far? What code do you have that already produces this output? – Prune Dec 01 '17 at 00:28
  • it is a stream data from a GPS module and so far I haven't been able to find a library to parse it so I though to do it that way trying to stop it because the stream keeps on coming – GreenRunsDeep Dec 01 '17 at 01:41
  • Make it raw string and then split it by [,] if you are having issue while parsing. After that create two list, in one list add all elements and in other add if 'b in word and then keep tracking the len of that second list as soon it cross the len 5 break the loop or sys.exit() – Aaditya Ura Dec 01 '17 at 04:02
  • What have you tried? Show us some code... – dda Dec 03 '17 at 18:02

2 Answers2

0

You can try regex and catch the group(),

Are you looking for something like this:

import re
pattern=r"b'[0-9]+"

data="""b'30,22,37,293,29,26,55,30D\r\n$GPGSV,3,1,11,01,04,250,,03,21,309,24,08,15,200,,10,20,157,*7B\r\n$GPGSV,3,2,11,14,49,062,37,16'
b'8,W,001328.00,A,A*70\r\n9,26,55,359,30*75\r\n$GPGSV,3,3,11,27,40,170,38,31,20,022,14,32,42,094,35*4C\r\n$GPGLL,0226.72251,N,0'
b'A*2E\r\n$GPGGA,001329.00,A,A*70\r\n$GPRMC,001329.00,A,0226.72248,N,07635.92031,W,0.008,,011217,,,A*64\r\n$GPVTG,,T,,M,0.008,N,'
b'2.25,1.08,1.98*0D\r\n$GPG1329.00,0226.72248,N,07635.92031,W,1,08,1.08,1769.1,M,9.8,M,,*4C\r\n$GPGSA,A,3,16,32,26,31,14,22,27'"""

all_items=[]
track=[]
for i in data.split(','):
    pr=re.search(pattern,i)
    if len(track)==5:
        break
    else:
        if pr!=None:
            track.append(pr.group())
            all_items.append(pr.group())
        else:
            all_items.append(i)

print(all_items)

Let's try instead of 5 , if second "b'" shows up:

all_items=[]
track=[]
for i in data.split(','):
    pr=re.search(pattern,i)
    if len(track)==2:
        break
    else:
        if pr!=None:
            track.append(pr.group())
            all_items.append(pr.group())
        else:
            all_items.append(i)

print(all_items)

Output:

["b'30", '22', '37', '293', '29', '26', '55', '30D\r\n$GPGSV', '3', '1', '11', '01', '04', '250', '', '03', '21', '309', '24', '08', '15', '200', '', '10', '20', '157', '*7B\r\n$GPGSV', '3', '2', '11', '14', '49', '062', '37', "b'8"]

Instead of 3 you can replace 5 there.

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
0
import re
from machine import UART
uart = UART(2, 9600)

pattern=r"b'[0-9]+"

while True:
  if uart.any():
    data=uart.read()
    all_items=[]
    track=[]
    for i in data.split(','):
        pr=re.search(pattern,i)
        if len(track)==5:
            break
        else:
            if pr!=None:
                track.append(pr.group())
                all_items.append(pr.group())
            else:
                all_items.append(i)

    print(all_items)



this would be the code apllied to my project, but I get this error:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 12, in <module>`enter code here`
TypeError: can't convert 'str' object to bytes implicitly