2017-01-23 37 views
-1
if "\\" in item["message"]: 
     item["message"] = str(item["message"]).replace("\\", "\\\\").encode("utf-8") 

寫程序,有許多「\」項[「消息」],因此,當試圖插入此在MySQL表項,它的錯誤,我嘗試應對項目[「消息」]的「\」,所以我寫上面的程序,蟒:「」 str中一個字節狀物體是必需的,而不是「STR」

,當它運行時,它的錯誤:

Traceback (most recent call last): 
    File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 237, in <module> 
    group_download.group_feed_storage(FB_access_token,group_id,group_name) 
    File "C:\Python\PyCharmProject\FaceBookCrawl\group_download.py", line 116, in group_feed_storage 
    mysql_manage().group_feed_db_manage(type,group,name,feed) 
    File "C:\Python\PyCharmProject\FaceBookCrawl\database_manage.py", line 95, in group_feed_db_manage 
    if "\\" in item["message"]: 
TypeError: a bytes-like object is required, not 'str' 

Process finished with exit code 1 

你能幫我一下嗎?

+0

什麼是'type(item [「message」])? –

回答

0

item['message']是一個字節 - 像對象一樣,所以它可能包含特定的字節序列,但不包含子字符串。搜索類似字節的對象:

if b"\\" in item["message"]: 
^
+0

我按照你所說的修改了程序,但是又出錯了,請看我的回答如下 – bin

+0

@bin - 這是你的問題中沒有描述過的一個不同的錯誤。如果您遇到一個您無法通過大量研究解決的新問題,請提出一個新問題。不要將更新發布到您的問題上作爲答案。 – TigerhawkT3

相關問題