Python Forum
Find string between two substrings, in a stream of data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find string between two substrings, in a stream of data
#1
I have this continuous serial data stream:

----------------------------------------

SENSOR COORDINATE         = 0

MEASURED RESISTANCE       = 3.70 kOhm

----------------------------------------

----------------------------------------

SENSOR COORDINATE         = 1

MEASURED RESISTANCE       = 3.70 kOhm

----------------------------------------

----------------------------------------

SENSOR COORDINATE         = 2

MEASURED RESISTANCE       = 3.69 kOhm

----------------------------------------
For each iteration, i want to be able to grab the values. The sensor coordinate value, and the resistance value.

I found solutions using
.split()
and with regular expressions, but the problem is that in my case, there is not one string that i want to filter, but a continuous stream.

For example,
.split()
will find my string, but it will split the stream in half. This does not work, in a continuous stream, for more than one time.
Reply
#2
How are you reading the data? As long as you have some sort of iterator for each line, you can check each line for one of your data components.

for line in stream:
    key,value = line.rstrip().split("=")
    if "SENSOR COORDINATE" in key:
        sensor = int(value)
    elif "MEASURED RESISTANCE" in key:
        resistance = int(value)

    # Act on data here between receiving new lines...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stream via socket with multiple clients principemestizo 0 155 Nov-01-2021, 06:25 PM
Last Post: principemestizo
  Decoding a serial stream AKGentile1963 7 1,680 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Regular expression: cannot find 1st number in a string Pavel_47 2 910 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Microphone stream manipulation Talking2442 0 935 Jan-07-2021, 07:36 PM
Last Post: Talking2442
  Using lambdas and map() to parse substrings in a single line Drone4four 5 1,445 Sep-20-2020, 10:38 AM
Last Post: snippsat
  Best Video Quality And Stream Harshil 2 822 Aug-19-2020, 09:03 AM
Last Post: Harshil
  Find data using a period of time in SQLITE3 SmukasPlays 2 924 Jul-30-2020, 02:02 PM
Last Post: SmukasPlays
  find a string in a field in MongoDB Leon79 2 942 Jul-19-2020, 09:20 PM
Last Post: menator01
  Help to find a string and read the next lines crlamaral 4 1,290 Mar-19-2020, 09:24 AM
Last Post: Larz60+
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 951 Mar-03-2020, 04:47 AM
Last Post: davidshq

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020