我的cmd Python中的语法错误在哪里?

我的cmd Python中的语法错误在哪里?,python,Python,几个小时以来,我的命令出现了一些错误: message = "test."+hostname+"."+service+" "current_state_id "%d\n" % int(time.time()) 错误是: SyntaxError:无效语法 结果将是 message=test.myhost.myservice 2 1467383079 问题出在哪里?您至少有两个问题;当前\u状态\u id与其前面的空格或后面的字符串之间没有任何内容 在代码中使用字符串替换时,您似乎了解字符串替换

几个小时以来,我的命令出现了一些错误:

 message = "test."+hostname+"."+service+" "current_state_id "%d\n" % int(time.time())
错误是:

SyntaxError:无效语法

结果将是 message=test.myhost.myservice 2 1467383079


问题出在哪里?

您至少有两个问题;
当前\u状态\u id
与其前面的空格或后面的字符串之间没有任何内容

在代码中使用字符串替换时,您似乎了解字符串替换;您应该在整个过程中使用它,而不是使用串联和替换的组合

message = "test.%s.%s %s %d\n" % (hostname, service, current_state_id, int(time.time()))
或者使用新的
格式
方法:

message = "test.{}.{} {} {}\n".format(hostname, service, current_state_id, int(time.time()))
有两个错误:

1) 在''''和'current_state_id'之间需要一个'+'

2) “当前状态\u id”和“%d\n”之间需要一个“+”

这是正确的版本:

message=“test.”+主机名+“+服务+”“+当前状态\u id+%d\n”%int(time.time())


Thx它的作品,我是一个初学者在Python。您的行末尾缺少a。)。您缺少
”、
当前状态\u id
%d\n”
之间的
+
s