繁体   English   中英

python:"\\" in str 需要一个类似字节的对象,而不是 'str'

[英]python:"\" in str a bytes-like object is required, not 'str'

   if "\\" in item["message"]:
       item["message"] = str(item["message"]).replace("\\", "\\\\").encode("utf-8")

写程序,item['message']中有很多“\\”,所以在mysql表中插入这个item时报错,我尝试处理item[“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

你能帮我吗

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM