在 mqtt 中发布一个浮点数列表,这个 returns 我是一个字符串列表

publication of a list of float in mqtt, this returns me a list of string

我是 python 和 mqtt 连接的新手,我正在尝试从 node-red 向 mqtt 连接发送一个浮动坐标数组。

[ 1.569326954184064e-16, 0.002832536751320802, 0.0009944428903551428, 0.0020108790027094717, 0.0005157974272640295, 0.0015507524397321535, 0.0019721403935832175, 0.0020006052222184875, 0.003397701323871767, 0.0018728627212459418 … ]

当我收到它的数据时,我收到一个字符串列表,每个字符都在列表的一个元素中。

def on_message(client, userdata, message):
 print("received message =",str(message.payload.decode("utf-8")))
 analyse(message.payload.decode("utf-8"))

def analyse(donnee):
 print("data")
 print(donnee)
 print(type(donnee[5]))
 print((donnee[0]))
 print((donnee[1]))
 print((donnee[2]))
 print((donnee[3]))
 print((donnee[4]))
 print((donnee[5]))

我在 return

中有这个
received message = [1.569326954184064e-16,0.002832536751320802,0.0009944428903551428,0.0020108790027094717,0.0005157974272640295,0.0015507524397321535,0.0019721403935832175,
data
[1.569326954184064e-16,0.002832536751320802,0.0009944428903551428,0.0020108790027094717,0.0005157974272640295,0.0015507524397321535,0.0019721403935832175
<class 'str'>
[
1
.
5
6
9

我想知道如何在 message.payload 列表中获取浮动格式的坐标。像这样:

print((donnee[0]))
print((donnee[1]))

1.569326954184064e-16
0.002832536751320802

感谢您的帮助

使用ast.literal_eval计算文字表达式:

import ast

def analyse(donnee):
   donnees = ast.literal_eval(donnee)
   return donnees

data = b'[1.569326954184064e-16,0.002832536751320802,0.0009944428903551428,0.0020108790027094717,0.0005157974272640295,0.0015507524397321535,0.0019721403935832175]'
print(analyse(data.decode('utf-8')))

# Output
[1.569326954184064e-16, 0.002832536751320802, 0.0009944428903551428, 0.0020108790027094717, 0.0005157974272640295, 0.0015507524397321535, 0.0019721403935832175]