【发布时间】:2020-02-27 08:46:23
【问题描述】:
我正在尝试使用 telnetlib 与我的 yeelight 台灯进行通信,使用如下命令:{"id":1,"method":"set_power","params":["on","smooth",500]}我得到这样的错误:
tn.write("{"id":1,"method":"set_power","params":["off","smooth",500]}")
^
SyntaxError: invalid syntax
我的代码是:
import time
import telnetlib
HOST ="192.168.1.100"
tn=telnetlib.Telnet(HOST,55443)
tn.write("{"id":1,"method":"set_power","params":["off","smooth",500]}")
l=tn.read_all()
【问题讨论】:
-
您的字符串在它的旁边有双引号,所以它不能被这些相同的引号括起来。要么转义字符串中的引号(
"\"..."),要么使用另一种机制来引用,例如单引号',或者使用 json dict / list 对数据进行建模,并使用json.dumps()编组为 json。