TypeError: write() argument must be str, not bytes UnicodeDecodeError: 'gbk' codec can't decode byte

运行python3报错:

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

或者

UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

出错原因: Python3给open函数添加了名为encoding的新参数,而这个新参数的默认值却是‘utf-8’。这样在文件句柄上进行read和write操作时,系统就要求开发者必须传入包含Unicode字符的实例,而不接受包含二进制数据的bytes实例。

示例1:

   fw = open(filename,'w')  

变为

   fw = open(filename,'wb')  

示例2

    fr = open(filename,'r')

变为

    fr = open(filename,'rb')

你可能感兴趣的:(报错解决方案,python,python3,机器学习实战,TypeError)