python:“\”在str中需要一个类似字节的对象,而不是'str'

时间:2017-01-23 02:39:08

标签: python

   if "\\" in item["message"]:
       item["message"] = str(item["message"]).replace("\\", "\\\\").encode("utf-8")
编写程序,在项目['message']中有很多“\”,因此,当试图在mysql表中插入这个项目时,它出错了,我试着在项目[“message”中处理“\” “],所以我写上面的程序,

运行时,错误:

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

你可以帮助我吗

1 个答案:

答案 0 :(得分:0)

item['message']是一个类似字节的对象,因此它可能包含某些字节序列,但不包含子字符串。搜索类似字节的对象:

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