首页 > 文章列表 > python open函数中文乱码怎么解决

python open函数中文乱码怎么解决

Python 中文乱码 open函数
225 2022-08-07

首先在D盘下新建一个html文档,接着在里面输入含有中文的Html字符,使用中文格式对读取的字符进行解码,再用utf-8的模式对字符进行编码,然后就能正确输出中文字符。

代码如下:

# -*- coding: UTF-8 -*-
file1 = open("D:/1.html", mode='rb+')
data = file1.read().decode('gbk').encode('utf-8')