'How can I get the most specific value from a data stream?
I tried to program a server (UDP Socket) to allow a data exchange between my PC and a sensor in testing machine. I receive from a sensor continually a data like this:
0.00;0.966458;4.75;0.000160
0.00;0.976958;4.75;0.000160
S reached (1)
0.00;0.986958;4.75;0.000160
0.00;1.001051;-2.25;0.000160
Each second I receive one or more lines. It depends on frequency, which I setup. All data, which I send to machine (commands) or receive (measured data from sensor) are "strings".
My goal: as soon I receive from a sensor: "S reached (1)", I want to save the next received line "0.00;0.986958;4.75;0.000160" as a variable. In my case the second value in this line shall be saved as a variable, "0.00;0.986958;4.75;0.000160".
As a first step I try to do something easier. When I receive "S reached (1)" the server has to do something.
while True:
data, address = socket.recvfrom(1024)
data = data.decode("utf-8")
print(data)
if data == "S reached (1)":
print("Hello")
Unfortunately I don't get "Hello".
Solution 1:[1]
There is probably some trailing whitespace. Stripping it off should solve the problem:
data = data.decode("utf-8").strip()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luke Smith |
