TypeError: write() argument must be str, not bytes

w文件打开以 ‘二进制‘  方式:

with open(‘teacher.html‘,‘wb+‘) as f:
    f.write(response.body)

要写入"中文",防止乱码:

fo = open("temp.txt", "wb+")
str = ‘中文‘
str = str.encode(‘utf-8‘)
fo.write(str)
fo.close()

  

  

时间: 2024-08-09 17:51:42

TypeError: write() argument must be str, not bytes的相关文章

Python错误TypeError: write() argument must be str, not bytes

2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not bytes 网上搜索才发现原来是文件打开的方式有问题. 之前文件打开的语句是: f=open("list.pkl","w+") 然后使用二进制方式打开就没有这个问题: f=open("list_account.pkl","wb+") 产

Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+

今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MEDIA_ROOT,pic1.name) with open(picName,'w') as pic: for c in pic1.chunks(): pic.write(c) return HttpResponse(picName) 出现TypeError: write() argument must

TypeError: write() argument must be str, not bytes报错

TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: 然后使用二进制方式打开就没有这个问题: with open('C:/result.pk','wb+') as fp: 产生问题的原因是因为存储方式默认是二进制方式. 原文地址:https://www.cnblogs.com/cheng10/p/10107838.html

Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io import BytesIO as StringIO   Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

Python_异常:TypeError: write() argument must be str, not list

文件写入操作时,报错:TypeError: write() argument must be str, not list 原因:python写入的内容要是字符串类型的 上代码: fp = open("a.txt","w")fp.write([1,2,3])fp.close() >>> fp = open("a.txt","w") >>> fp.write([1,2,3]) Traceback

python TypeError: must be str, not bytes错误

1 TypeError: must be str, not bytes错误: 2 3 解答: 4 写文件处 f=open(filename, 'w')应该写为 open(filename, 'wb') 5 读文件时 f=open(filename,'rb') UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence解决方法同上

TypeError: the JSON object must be str, not 'bytes'

json.loads(json_data)报错 修改为json.loads(json_data.decode())即可 一些修改为load什么的方法会带来新的报错… 直接添加decode()解决 描述 Python decode() 方法以 encoding 指定的编码格式解码字符串.默认编码为字符串编码. 语法 decode()方法语法: str.decode(encoding='UTF-8',errors='strict') 参数 encoding -- 要使用的编码,如"UTF-8&quo

HTTPResponse object — JSON object must be str, not 'bytes'

http://stackoverflow.com/questions/24069197/httpresponse-object-json-object-must-be-str-not-bytes HTTPResponse object — JSON object must be str, not 'bytes' up vote17down votefavorite 7 I've been trying to update a small Python library called libpyne

Python3 中的 str 和 bytes

Python3 中的 str 和 bytes 与 Python2.X 不同,Python3.X 严格区分了 str 和 bytes 两种类型.文本为 Unicode,由 str 类型表示:二进制数据则由 bytes 表示. Python3.X 不会以任意隐式的方式混用 str 和 bytes.因此使用者不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然). 例如 Python3.X 中的socket.send()函数,如果传入未编码的字