Python教程

python读取文件时UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte

本文主要是介绍python读取文件时UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

问题描述:
python读取log文件时,由于文件中有特殊字符,抛异常:UnicodeDecodeError: ‘gbk’ codec can’t decode byte
解决办法:读取文件时添加编码参数,使用utf-8编码,如下

file_data = open(file=inputfile, mode='r', encoding="utf-8")
这篇关于python读取文件时UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!