Python encountered a problem reading the file!

CSDN Q & A 2022-05-15 04:47:04 阅读数:990


#! Problems encountered :python Read txt When text , Why is it set to... When opening a file utf-8 Read the file in the encoding format , You also need to set the encoding format of the text to utf-8? The question is Python The interpreter has been set utf-8 Coding format , Why change the format of the text ?
#python Code


# Running results


# Text file format


Take the answer :

This is because when you don't know what encoding the file is , Change unity to utf-8 To read (python Read ) And write ( file save )
It's like Chinese and Japanese , Neither can understand what the other is saying , Then unify one language ( English ) To communicate .
If you know in advance what code the file is saved in , such as gbk, You don't have to change the file format , stay python Inside directly encoding='gbk' You can read it .

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。