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解决方法同上
时间: 2024-08-09 10:28:53

python TypeError: must be str, not bytes错误的相关文章

python"TypeError: 'NoneType' object is not iterable"错误解析

尊重原创博主,原文链接:https://blog.csdn.net/dataspark/article/details/9953225 [解析] 一般是函数返回值为None,并被赋给了多个变量. 实例看下: c=0def test(): if c == 1: a = b = 1 return a, b a, b = test() 使用 a, b = test()调用时,就会报错:TypeError: 'NoneType' object is not iterable 在Python判断语句中,当

Python 3中的str和bytes类型

Python3 中的str和bytes类型 Python3最重要的新特性之一是:对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python3不会以任何隐式的方式混用str和bytes,我们不能拼接字符串和字节流,也无法在字节流里搜索字符串(反之亦然),也不能将字符串传入参数为字节流(反之亦然). 编码发展的历史 在bytes和str之前,需要先说说关于编码的发展.在计算机历史的早期,美国为代表的英语系国家主导了整个计算机行业,2

保存网页TypeError: must be str, not bytes

问题: import urllib.request import sys resp=urllib.request.urlopen("http://www.baidu.com") html=resp.read() fo=open("test.html","w") fo.write(html) fo.close() Python 保存网页,后出现如下错误 解决方法: import urllib.request import sys resp=urll

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

python判断字符串,str函数isdigit、isdecimal、isnumeric的区别

s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() 所有字符都是大写s.istitle() 所有单词都是首字母大写,像标题s.isspace() 所有字符都是空白字符.\t.\n.\r 判断是整数还是浮点数a=123b=123.123 >>>isinstance(a,int)True>>>isinstance(b,floa

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+") 产

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

python——TypeError: 'str' does not support the buffer interface

import socket import sys port=51423 host="localhost" data=b"x"*10485760 #在字符串前加 b 是字符串变为bytes类. sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((host,port)) byteswritten=0 while byteswritten<len(data): startpos =

小白的Python之路 day1 Python3的bytes/str之别

原文:The bytes/str dichotomy in Python 3 Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰.你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然).这是件好事. 不管怎样,字符串和字节包之间的界线是必然的,下面