Jan-28-2020, 04:39 PM
Hello everyone
I am quite stuck. i am trying to get a message from a TCP communication protocol, this message is converted in a list of strings and then into a list of float, The thing is that im getting an error because of the delimiters.
<the message is dynamic and it has this form:
message = 0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000*
i want to get everything behind the '*'
here is my code
message: 0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000
message: 0.2593872,0.0000000,-5.0712390,0.2593872,0.0000000,-0.4532299,0.2593872,0.0000000,-0.1035
I am quite stuck. i am trying to get a message from a TCP communication protocol, this message is converted in a list of strings and then into a list of float, The thing is that im getting an error because of the delimiters.
<the message is dynamic and it has this form:
message = 0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000*
i want to get everything behind the '*'
here is my code
while True: message = conn.recv(BUFFER_SIZE) if not message: break t1 = time.time() message = message.decode("utf-8") print("Raw message: {}".format(message)) data = list(message.split(",")) print("Data Size:{} ".format(len(data))) MyNewData = [] for item in data: MyNewData.append(float(item)) print("The new list: {}".format(MyNewData)) print("Size:{} ".format(len(data))) if len(MyNewData) < 9: continue DataX = MyNewData[0:3] DataY = MyNewData[3:6] DataZ = MyNewData[6:9]This the ouput and error
Output:Raw message: 0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000*
expected output should be like thismessage: 0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000,0.2081006,0.0000000,0.0000000
Error: File "lstm_server_3.py", line 158, in <module>
MyNewData.append(float(item))
ValueError: could not convert string to float: '0.0000000*'
The i changed the code into thiswhile True: message = conn.recv(BUFFER_SIZE) if not message: break t1 = time.time() message = message.decode("utf-8") print("Raw message: {}".format(message)) data = list(message.split(",")) print("Data Size:{} ".format(len(data))) MyLastString = data[-1] result2 = MyLastString.find('*') if result2 > 0: MyLastString = MyLastString[0:result2] data[len(data)-1] = MyLastString MyNewData = [] for item in data: MyNewData.append(float(item)) print("The new list: {}".format(MyNewData)) print("Size:{} ".format(len(data))) if len(MyNewData) < 9: continue DataX = MyNewData[0:3] DataY = MyNewData[3:6] DataZ = MyNewData[6:9]Now the error is in the first position of the list. Also it seems that some part of the last element is in the first element
Output:message: 35*0.2593872,0.0000000,-5.0712390,0.2593872,0.0000000,-0.4532299,0.2593872,0.0000000,-0.10
so my expected output should be like thismessage: 0.2593872,0.0000000,-5.0712390,0.2593872,0.0000000,-0.4532299,0.2593872,0.0000000,-0.1035
Error: File "lstm_server_3.py", line 158, in <module>
MyNewData.append(float(item))
ValueError: could not convert string to float: '35*0.2593872'