在变量上使用strip()的问题

时间:2015-05-21 02:00:03

标签: python python-3.x twitch urllib3

我正在尝试删除字符b,'()。 我遇到的问题是它说TypeError'st'不支持缓冲接口。

以下是代码的相关部分:

import urllib3
def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    rawData = r.data
    liveTime = bytes(rawData.strip("b,\'()", rawData))

    message = "Tripped has been live for: ", liveTime
    send_message(CHAN, message)

2 个答案:

答案 0 :(得分:3)

你拥有的是二进制数据。它不是一个字符串。你需要先解码它。

此外,您无需在条带方法中将rawData传递给自身。

import urllib3

def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    strData = r.data.decode('utf-8')
    liveTime = strData.strip("b,\'()")

    message = "Tripped has been live for: %s" % liveTime
    print(message)

command_uptime()

还要注意,您的message变量是元组而不是字符串。我不知道send_message是否期望这样。我把它格式化为一个字符串。

答案 1 :(得分:0)

删除第二个参数。

import urllib3
def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    rawData = r.data
    liveTime = bytes(rawData.strip("b,'()"))

    print("Tripped has been live for: %s" % liveTime)


command_uptime()

输出:

Tripped has been live for:  1 hour, 18 minutes