第4章 4.4 处理编码
作者:互联网
1. 用默认方式打开:
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ echo $LANG
zh_CN.UTF-8
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cat example_iso.txt
20�(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cat example_utf8.txt
20£
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('example_utf8.txt') as file:
... print(file.read())
...
20£
#utf-8编码的文件可以打开
>>> with open('example_iso.txt') as file:
... print(file.read())
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/metal/anaconda3/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 2: invalid start byte
#iso编码的文件用默认编码打开报错。
标签:4.4,编码,venv,...,处理,PAutomationCookbook,metal,file,txt 来源: https://www.cnblogs.com/DreamToAwakening/p/16138581.html