UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x91 in position 151: illegal multibyte sequence

介绍


使用Python操作文件时报错

UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x91 in position 151: illegal multibyte sequence

代码


settings=open(os.path.join(os.getcwd(),'web','superman.json') ,'r').read()

服务报错内容


RROR:pywebio.session.base:Error
Traceback (most recent call last):
  File "C:\Users\code404\PycharmProjects\OpsGuide\venv\lib\site-packages\pywebio\session\threadbased.py", line 86, in main_task
    target()
  File "C:\Users\code404\PycharmProjects\OpsGuide\web\guide.py", line 9, in bmi
    settings=open(os.path.join(os.getcwd(),'web','superman.json') ,'r').read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x91 in position 151: illegal multibyte sequence

原因


txt是utf-8编码,所以编码需要转为utf-8

把代码修改为


settings=open(os.path.join(os.getcwd(),'web','superman.json') ,'r',encoding='utf-8').read()

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/1355.html

发表评论

登录后才能评论