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
  Microphone stream manipulation Talking2442 1 2,716 Nov-19-2023, 02:08 PM
Last Post: palumanic
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,031 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  EEG stream data with mne and brainfolw PaulC 0 487 Aug-22-2023, 03:17 AM
Last Post: PaulC
  get data from excel and find max/min Timmy94 1 1,107 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 1,150 Jul-25-2022, 10:03 AM
Last Post: korenron
  Substitue multiple substrings in one command Pavel_47 0 826 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  Find and Replace numbers in String giddyhead 2 1,219 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Decoding a serial stream AKGentile1963 7 8,509 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,406 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Using lambdas and map() to parse substrings in a single line Drone4four 5 3,042 Sep-20-2020, 10:38 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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