python3.6使用chardet模块总是报错ValueError: Expected a bytes object, not a unicode object

py3里,字符串,str类型,是unicode编码格式。其他类型都是byte,编码格式是gbk,utf-8等

而chardet是检查byte类型的编码格式的,不是检查str类型的编码格式的。

a="abc啊”,是字符串,就不能用chardet了。

当获取的数据(byte)类型,需要当成字符串来处理,需要先将其编码成unicode(python处理的都是此编码)。

而要将bytes转换成unicode,需要先知道bytes具体是什么编码格式如gbk,然后使用aa=decode("gbk"),aa为unicode的该数据内容。

如果很不幸,你有一堆bytes,不知道它们的编码(例如 网站服务器返回的响应体),
这时候,你就需要chardet 来测试它们的编码。

时间: 2024-07-30 12:40:46

python3.6使用chardet模块总是报错ValueError: Expected a bytes object, not a unicode object的相关文章

python3 load Iris.data数据集出现报错key words: b'Iris-setosa'

通过搜索原因,发现有可能是在对文件读取是编译出现了问题,并且Keyword中提示b'Iris-setosa',而我们的string转float函数中没有字母b,很奇怪.所以尝试将转换函数所有的string前加b.结果发现数据读取正常.下边附上转换函数: def iris_type(s): it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2} return it[s] python3 load Iris.data

selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string

在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.csdn.net/detail/ztzy520/9725887 或https://github.com/mozilla/geckodriver/release 如:geckodriver-v0.14.0-win32 Chrome:chromedriver 下载网址:http://chromedriver

matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfullpathname: embedded null character in path 原因以及Solution: http://stackoverflow.com/questions/34004063/error-on-import-matplotlib-pyplot-on-anaconda3-f

keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'

在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)' 原因:服务器和本地的运行环境配置不同 查看本地keras版本: In [1]: import kera

tensorflow读取jpg格式图片报错 ValueError: Only know how to handle extensions: ['png']; with Pillow installed matplotlib can handle more images

当运行mpimg.imread("img.jpg")时,spyder 出现如下错误: ValueError: Only know how to handle extensions: ['png']; with Pillow installed matplotlib can handle more images 解决办法就是  安装pillow即可 我是在anaconde里安装的很简单,执行如下步骤即可: tensorflow读取jpg格式图片报错 ValueError: Only kn

前端控制台 JavaScript函数报错 SyntaxError: expected expression, got ';' SyntaxError: expected expression, got 'if'

在火狐浏览器下调试时, 页面报错SyntaxError: expected expression, got ';'或者SyntaxError: expected expression, got 'if'等 其实就是js部分某个变量没有赋值到,例如: // 这种echo在前端输出的是空 var test = <?php echo 1 > 0 ? true : false ?>; 在前端js显示的是: var test = ; 注意: 在前端 test = ; 这样的变量没有赋值到就会报错,

Windows下安装模块mysqlclient报错处理

Windows环境下使用pip命令安装Python模块mysqlclientpip install mysqlclient安装过程报错  [error] Microsoft Visual C++ 14.0 is required 于是更换另外一种方法安装,到这个网站上下载mysqlclient安装包https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 由于本地安装的是python3.6版本,于是下载 mysqlclient-1.3.13-cp36-c

python模块安装报错大全

报错 环境 解决 手动安装pip install mysqlclient 报错: _mysql.c(29) : fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory no module named MySQLdb win7 py2.7 下载https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient 安装pip install mysql

前端QRCode.js生成二维码(解决长字符串模块和报错问题)

QRCode 用法 1.使用npm安装到你的项目中 npm install qrcode2 --save 使用commonjs或者es6模块方式导入 var QRCode = require('qrcode2'); // 或者 import QRCode from 'qrcode2'; 3 . 实例化QRCode对象 new QRCode(document.getElementById('qrcode'), 'http://www.baidu.com'); // // 或者配置一些选项 var