Python 出现 can't use a string pattern on a bytes-like object
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 出现 can't use a string pattern on a bytes-like object相关的知识,希望对你有一定的参考价值。
- # coding=utf-8
- import urllib.request
- import re
- url = ‘http://www.163.com‘
- file = ‘d:/test.html‘
- data = urllib.request.urlopen(url).read()
- r1 = re.compile(‘<.*?>‘)
- c_t = r1.findall(data)
- print(c_t)
发现读取下来后,运行到第9 行,出现:
can‘t use a string pattern on a bytes-like object
查找了一下,是说3.0现在的参数更改了,现在读取的是bytes-like的,但参数要求是chart-like的,找了一下,加了个编码:
data = data.decode(‘utf-8‘)
在与正则使用前,就可以正常使用了..
以上是关于Python 出现 can't use a string pattern on a bytes-like object的主要内容,如果未能解决你的问题,请参考以下文章
fedora26 编译内核出现Can't use 'defined(@array)' 错误
Python idle运行代码出现'ascii' codec can't encode characters in position 0-2
Can't use Subversion command line client: svn
Python3中出现UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in ra
pythonPython3中出现'gbk' codec can't encode characte的成功解决方法?
python3安装xadmin出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 344