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
  find the average data from everyone in the same year STUdevil 11 2,060 Oct-22-2024, 02:30 PM
Last Post: deanhystad
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,917 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  EEG stream data with mne and brainfolw PaulC 0 974 Aug-22-2023, 03:17 AM
Last Post: PaulC
  get data from excel and find max/min Timmy94 1 1,787 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 1,858 Jul-25-2022, 10:03 AM
Last Post: korenron
  Substitue multiple substrings in one command Pavel_47 0 1,316 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  Find and Replace numbers in String giddyhead 2 2,895 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Decoding a serial stream AKGentile1963 7 12,749 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Regular expression: cannot find 1st number in a string Pavel_47 2 3,057 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Microphone stream manipulation Talking2442 0 3,800 Jan-07-2021, 07:36 PM
Last Post: Talking2442

Forum Jump:

User Panel Messages

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