Python Forum
Find string between two substrings, in a stream of data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find string between two substrings, in a stream of data (/thread-33588.html)



Find string between two substrings, in a stream of data - xbit - May-08-2021

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.


RE: Find string between two substrings, in a stream of data - bowlofred - May-09-2021

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