TypeError: Can't convert 'int' object to str implicitly Python3.5.2 on Ubuntu 16.04 Server

TypeError: Can't convert 'int' object to str implicitly Python3.5.2 on Ubuntu 16.04 Server

我是 Python 的新手。我在添加

后立即出现一些错误
device_purpose = device["purpose"].rstrip()

我已经尝试找到解决方案,但我还是不明白。

[第 118 行]

for alert in alerts["alerts"]:
    alert_rule = librenms_api.get_alert_rule(alert["rule_id"])
    device = librenms_api.get_device(alert["device_id"])
    device_status = device["status"]
    device_status_reason = device["status_reason"]
    device_hostname = librenms_api.translate_device_ip_to_sysname(device)
    device_location = re.sub(r'\[.*\]', '', device["location"]) # remove gp$
    alert_severity = alert["severity"]

[第 61 行]

 def get_alert_rule(self,rule_id):
            rule_req = urllib.Request(self.api_url + "rules/" + rule_id, he$
            rule_contents = urllib.urlopen(rule_req).read()
            return json.loads(rule_contents)["rules"][0]

一个错误:

Traceback (most recent call last):
File "./open_alerts.py", line 118, in <module>
alert_rule = librenms_api.get_alert_rule(alert["rule_id"])
File "./open_alerts.py", line 61, in get_alert_rule
rule_req = urllib.Request(self.api_url + "rules/" + rule_id, headers=self.request_headers)
TypeError: Can't convert 'int' object to str implicitly

这来自 git 克隆 https://github.com/RaymiiOrg/librenms-api-alerts

你能帮帮我吗?谢谢你。

如果 rule_id 是一个 int 那么你应该转换为 string:

str(rule_id)

变化中:

rule_req = urllib.Request(self.api_url + "rules/" + rule_id, headers=self.request_headers)

rule_req = urllib.Request(self.api_url + "rules/" + str(rule_id), headers=self.request_headers)

我假设 rule_id 具有 int 值。尝试将其转换为字符串。

rule_req = urllib.Request(self.api_url + "rules/" + str(rule_id), headers=self.request_headers)