问题

读取txt文件中文乱码

with open('C:\\Users\\11342\\Desktop\\python\\1.txt') as f1:
    print(f1.read())

执行

解决

在open()中加入参数 encoding=‘UTF-8’ 就行了

with open('C:\\Users\\11342\\Desktop\\python\\1.txt',encoding='UTF-8') as f1:
    print(f1.read())

open() 方法

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。

注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。
open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file: 必需,文件路径(相对或者绝对路径)。
mode: 可选,文件打开模式
buffering: 设置缓冲
encoding: 一般使用utf8
errors: 报错级别
newline: 区分换行符
closefd: 传入的file参数类型
opener: 设置自定义开启器,开启器的返回值必须是一个打开的文件描述符。

更多推荐

python读取txt中文乱码