我有我的MQTT订阅者,需要保存输出,我尝试了不同的选项,但都不起作用。如你所见,我做了不同类型的测试。
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
#data = on_message() --no working
with open('data.json', 'a') as f:
print('file.txt', file=f)
#json.dump(data, f)
#f.write(print(msg.topic+" "+str(msg.payload)))
#data = on_message()
#with open ('data.json', 'w') as f:
# json.dump(data, f)
我是这样做的,如果有什么最好的方法请告诉我。谢谢。
def on_message(client, userdata, msg):
d = json.loads(msg.payload.decode("utf-8"))
with open("file.txt", "a+") as f:
with redirect_stdout(f):
print(msg.topic+" "+str(msg.payload))