current position:Home>Python encountered a problem reading the file!

Python encountered a problem reading the file!

2022-05-15 04:47:04CSDN Q & A

#! 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

img

# Running results

img


# Text file format

img




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 .


copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205041653539429.html

Random recommended