Skip to content
Advertisement

string.replace() outputs me an error when defining it

Im getting data from an API. Then i want to replace the value in the string from “True” to “Online”.

request = requests.get("https://api.cleanvoice.ru/ts3/?address=xxxxxxxxxxx")
request_text = request.text
data = json.loads(request_text)
status = data["can_connect"]

onoff = status.replace("True","Online")

The error i get from replace() is AttributeError: 'bool' object has no attribute 'replace'

I want to implement a Teamspeak Online Checker by getting the value if the server is online of some russian api. That by the way, works perfectly fine.

Im working on Python 3.7.6

Advertisement

Answer

The status variable appears to be a boolean one, as evidenced by the error:

AttributeError: ‘bool’ object has no attribute ‘replace’

You therefore should be able to just use something like:

onoff = "online" if status else "offline"
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement