TypeError: write() argument must be str, not bytes
程序员文章站
2024-02-21 23:12:49
...
今天在练习写网络爬虫时,出现如下错误:
TypeError: write() argument must be str, not bytes
出现问题的代码如下:
with open('./baidu.txt', 'w') as fp:
fp.write(response.read())
解决办法1:
将打开文件方式更改为以二进制的方式,如:wb就可解决。
解决方法2:
将下面写文件的方式进行字符类型转换,如:str(response.read())
原因分析:
response.read()的内容是二进制形式,而文件打开方式是字符类型,故需要将两者进行统一。
上一篇: Git如何将文件重命名
推荐阅读
-
TypeError: write() argument must be str, not bytes
-
TypeError: argument 1 must be 2-item sequence, not int
-
pytorch gpu版本安装外部库报错 TypeError: LoadLibrary() argument 1 must be str, not None
-
TypeError: transpose(): argument 'dim0' (position 1) must be int, not tuple
-
解决报错:TypeError: argument should be integer or bytes-like object, not ‘str‘
-
TypeError: join() argument must be str or bytes, not ‘PosixPath‘
-
编译安卓源码提示:TypeError: argument should be integer or bytes-like object, not ‘str‘
-
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
-
TypeError: write() argument must be str, not bytes报错
-
Python报错:TypeError: the JSON object must be str, bytes or bytearray, not ‘dict‘