TypeError:无法在Ubuntu 16.04 Server上将'int'对象转换为隐式str3.5.2

时间:2019-07-18 04:22:55

标签: python python-3.x server ubuntu-16.04 mysql-python

我是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 clone https://github.com/RaymiiOrg/librenms-api-alerts

你能帮我吗?谢谢。

2 个答案:

答案 0 :(得分:0)

如果rule_idint,则应强制转换为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)

答案 1 :(得分:0)

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

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