【发布时间】:2021-07-15 07:25:28
【问题描述】:
目前的结论:
converted 文件的encoding 是utf-8->utf-8 big->ansi -> utf-8。在每次转换后重新打开文件。
观察一段时间后,没有这样的错误。
当我使用PyCharm调试.py文件时,same文件有时会出现UnicodeDecodeError,有时是正常的。我的操作系统是Windows 10,PyCharm版本是2020.3.3社区版。
错误如下:
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 301, in _on_run
r = r.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data
我尝试在文件头添加如下代码,但有时还是报错,如何解决?
#!/usr/bin/env Python
# coding=utf-8
我找到了另一种使用记事本保存为 UTF-8 文档的方法。我试过了,但有时还是会出错。
【问题讨论】:
-
default encoding of a Python module (or file) is UTF-8,可以验证PyCharm encoding settings。这意味着不必在文件顶部指定编码(问题中的最后两行在历史上曾使用过,但大多已变得不必要)。您应该只从 IDE 中更改编码。这个问题不是关于代码,而是在 IDE 中设置文件的编码。
-
@bad_coder 谢谢。在IDE编码设置中,项目编码和属性文件不是utf-8,已经改了。