TypeError: cannot use a string pattern on a bytes-like object
一劳永逸解决: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的更多相关文章
- 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 ...
- 爬虫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. ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- 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 ...
- python3 pycurl 出现 TypeError: string argument expected, got 'bytes' 解决方案
用pycurl请求指定链接并返回结果时出现 TypeError: string argument expected, got 'bytes' 错误 经过排查问题出现在使用StringIO的write ...
- int preg_match( string pattern
preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...
- 关于TypeError: strptime() argument 1 must be str, not bytes解析
关于TypeError: strptime() argument 1 must be str, not bytes解析 在使用datetime.strptime(s,fmt)来输出结果日期结果时, ...
- Cannot enlarge string buffer containing XX bytes by XX more bytes
在ELK的数据库报警系统中,发现有台机器报出了下面的错误: 2018-12-04 18:55:26.842 CST,"XXX","XXX",21106,&quo ...
- 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 ...
随机推荐
- (转)io各层次性能汇总及运行速度对比
io各层次性能汇总:以上图片可以清晰的解释io的运行效率 守护进程:持续保持运行着的程序 进程:放在内存中运行的程序 程序:代码文件,php,java
- Author: Jan Odvarko, www.janodvarko.cz
/* * Author: Jan Odvarko, www.janodvarko.cz */ FBL.ns(function() { with (FBL) { function HelloWorld ...
- ae(ArcEngine) java swing开发入门系列(1):开发环境和代码部署
前言:做ae开发大部分人都是用C#版,很少用到java版,本系列文章主要介绍java版ae开发的入门,对于ae接口的高级应用,可以看C#版相关文章 开发环境软件: Intellij IDEA 2018 ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- 利用bintray-release插件上传到Bintray- HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]问题解决
凡是网上教程 有个5678步的总有这样或者那样的坑. 上周撸了一个小工具准备上传到jcenter,方便管理以及以后使用.看了一下教程,短短几步,弄了很久. 按Hongyang的教程http://www ...
- tsung基准测试方法、理解tsung.xml配置文件、tsung统计报告简介
网上搜集的资料,资料来源于:http://blog.sina.com.cn/ishouke 1.tsung基准测试方法 https://pan.baidu.com/s/1Ne3FYo8XyelnJy8 ...
- mvc的help和functions语法
@helper show(int num ) { ) { @:存在 } else { @:不存在 } } @functions { /// <summary> /// 方法必须要求为静态 ...
- JS实现单向链表、双向链表、循环链表
https://cloud.tencent.com/developer/article/1114246 链表存储有序的元素的集合,但是和数组不同的是,链表中的元素在内存中的存储并不是连续的.每一个链表 ...
- js中替换字符串
function formatStr(str){ str=str.replace(/\r\n/ig,"<br/>"); return str; } 要注意两点: 要使用 ...
- Bootstrap历练实例:标签页内的下拉菜单
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...