2017-01-23 36 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

相关问题