当前位置: 代码迷 >> 综合 >> web框架读取模板文件时报错‘gbk‘ codec can‘t decode byte 0xaa in position 223: illegal multibyte sequence的解决方法
  详细解决方案

web框架读取模板文件时报错‘gbk‘ codec can‘t decode byte 0xaa in position 223: illegal multibyte sequence的解决方法

热度:1   发布时间:2024-01-14 12:10:07.0

windows文系统编码的文件,编码为gbk,python操作时设置读取时编码为gbk

读取文件操作时需要读取编码类型为utf-8,python操作时需要设置读取utf-8

解决方法:在文件的右括号前面加上:     , encoding='UTF-8'

    with open('./template/index.html', 'r') as file:file_data = file.read()

这时报错:'gbk' codec can't decode byte 0xaa in position 223: illegal multibyte sequence

修改代码为以下即可解决

    with open('./template/index.html', 'r', encoding='utf-8') as file:file_data = file.read()

  相关解决方案