我正在尝试在网址上使用re.sub,但是当我这样做时,我收到有关
的错误expected string or buffer
这是代码:
elif used_prefix and cmd == "cats" and self.getAccess(user) >=1 and len(args) == 0:
try:
url = "http://catfacts-api.appspot.com/api/facts"
f = urllib.request.urlopen(url)
data = json.loads(f.readall().decode("utf-8"))["facts"]
data = re.sub(r'\<.*?\>',"",data).replace("\\","")
room.message("Random Cat Fact: %s" % data)
except:
room.message((str(sys.exc_info()[1])))
print(traceback.format_exc())
假设删除[“围绕单词”]并删除“\”周围的单词“\”
答案 0 :(得分:2)
json.loads(f.readall().decode("utf-8"))["facts"]
是一个包含字符串的列表。
替换以下行:
data = json.loads(f.readall().decode("utf-8"))["facts"]
使用:
data = json.loads(f.readall().decode("utf-8"))["facts"][0]
顺便说一句,您无需转义<
,>
。