一劳永逸解决:TypeError: cannot use a string pattern on a bytes-like object

TypeError: cannot use a string pattern on a bytes-like object

python2和python3之间切换,难免会碰到一些问题,有些方法比如re模块的findall要求传入的是字符串格式的参数,urllib.request.urlopen(url).read()返回的是bytes类型(这个是python3中才有的类型,所以很多python2中的方法都相应更改了)的,这样传参就会报以上错误。

python3中Unicode字符串是默认格式(就是str类型),ASCII编码的字符串(就是bytes类型,bytes类型是包含字节值,其实不算是字符串,python3还有bytearray字节数组类型)要在前面加操作符b或B;python2中则是相反的,ASCII编码字符串是默认,Unicode字符串要在前面加操作符u或U

一劳永逸的解决方法就是根据你传进来的参数自动辨别编码格式,然后进行相应的解码,就搞定啦:

import chardet   #需要导入这个模块,检测编码格式
encode_type = chardet.detect(html)
html = html.decode(encode_type['encoding']) #进行相应解码,赋给原标识符(变量)
从str到bytes:调用方法encode().
编码是把Unicode字符串以各种方式编码成为机器能读懂的ASCII字符串
从bytes到str:调用方法decode().

原文链接

 
 
 
 

TypeError: cannot use a string pattern on a bytes-like object的更多相关文章

  1. TypeError: cannot use a string pattern on a bytes-like object的解决办法

    #!/usr/python3 import re import urllib.request def gethtml(url): page=urllib.request.urlopen(url) ht ...

  2. 爬虫python3:TypeError: cannot use a string pattern on a bytes-like object

    import re from common_p3 import download def crawl_sitemap(url): sitemap = download(url) links = re. ...

  3. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  4. Python 出现 can't use a string pattern on a bytes-like object

    Python 出现 can't use a string pattern on a bytes-like object 学习了:https://www.cnblogs.com/andrewleeeee ...

  5. python3 pycurl 出现 TypeError: string argument expected, got 'bytes' 解决方案

    用pycurl请求指定链接并返回结果时出现 TypeError: string argument expected, got 'bytes'  错误 经过排查问题出现在使用StringIO的write ...

  6. int preg_match( string pattern

    preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...

  7. 关于TypeError: strptime() argument 1 must be str, not bytes解析

    关于TypeError: strptime() argument 1 must be str, not bytes解析   在使用datetime.strptime(s,fmt)来输出结果日期结果时, ...

  8. Cannot enlarge string buffer containing XX bytes by XX more bytes

    在ELK的数据库报警系统中,发现有台机器报出了下面的错误: 2018-12-04 18:55:26.842 CST,"XXX","XXX",21106,&quo ...

  9. Python之scrapy框架之post传输数据错误:TypeError: to_bytes must receive a unicode, str or bytes object, got int

    错误名:TypeError: to_bytes must receive a unicode, str or bytes object, got int 错误翻译:类型错误:to_bytes必须接收u ...

随机推荐

  1. Redis特性之持久化机制

    持久化机制 Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化. Redis支持两种持久化方式: 1.snapshotting(快照)也是默认方式 ...

  2. Echarts的重点

    官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...

  3. 转:IOS程序之间的文件共享

    原文 System-Declared Uniform Type Identifiers One of the common tasks that an iOS developer has to do ...

  4. 零基础逆向工程15_C语言09_位运算

    1.汇编中的移位指令 算数移位指令 指令格式:SAL/SAR Reg/Mem, CL/Imm SAL(Shift Arithmetic Left):算数左移 SAR(Shift Arithmetic ...

  5. 微信小程序中的target和currentTarget区别

    最近在学习微信小程序相关知识,其中提到了两个属性target和currentTarget,其中target是指向触发事件的元素(常见于事件委托中),而currentTarget是指向捕获事件的元素(即 ...

  6. MFC CDialog/CDialogEx DoModal ALT

    Questions: I'm using MFC CDialog/CDialogEx to show a modal dialog with DoModal.usually it works with ...

  7. web APP到底和跨平台APP开发有什么区别?

    什么是web app? WebApp是指基于Web的系统和应用,其作用是向广大的最终用户发布一组复杂的内容和功能. 说的浅显易懂点儿就是因为移动互联网特别火爆,很多企业公司也都想拥有一个属于自己的ap ...

  8. nginx只允许域名访问网址,禁止ip访问

    修改nginx配置 文件 在server段里插入如下正则: if ( $host != 'www.baidu.com') { return 403; } 说明:如果访问讨还不是www.baidu.co ...

  9. js构造方法

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...

  10. SQL_关联映射

    关联映射:一对多/多对一 存在最普遍的映射关系,简单来讲就如球员与球队的关系: 一对多:从球队角度来说一个球队拥有多个球员 即为一对多 多对一:从球员角度来说多个球员属于一个球队 即为多对一 数据表间 ...