当前位置:   article > 正文

Python报UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xad in position的解决办法_gbk' codec can't decode byte 0xad in position 5459

gbk' codec can't decode byte 0xad in position 5459: illegal multibyte seque
  1. with open("input.txt") as f:
  2. for line in f:
  3. print(line)

按行读取文本文件时报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position...

python的open方法默认编码取决于平台,如果是windows平台,默认编码是gbk,如果文件是utf8编码,就会报这个错误。

解决办法:

  1. with open("input.txt",encoding='utf-8') as f:
  2. for line in f:
  3. print(line)

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/151121
推荐阅读
相关标签
  

闽ICP备14008679号