2014-01-11 74 views
1

我試圖在URL中使用應用re.sub但是當我做,我得到一個關於使用應用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()) 

假設刪除[「圍繞單詞」]並刪除「\」單詞「\」

回答

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] 

BTW,你不需要逃避<>