2024-06-07 07:42:23 发布
网友
我的老师教我们如何使用“exec”,但我犯了一个错误:
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe6 in position 1814: illegal multibyte sequence
我使用:
exec(open("somefile.py").read())
如何解决这个问题?
假设这是Python 3源代码,那么可能的编码是UTF-8(这是python3源代码的标准编码)。
如果是这样,将open("somefile.py")更改为open("somefile.py", encoding="utf-8")将显式指定编码,覆盖区域设置默认值,这将允许您正确地读入该值。
open("somefile.py")
open("somefile.py", encoding="utf-8")
对于惯用代码,您还需要使用with语句(以保证文件的确定性关闭),使其:
with
with open("somefile.py", encoding="utf-8") as f: exec(f.read())
假设这是Python 3源代码,那么可能的编码是UTF-8(这是python3源代码的标准编码)。
如果是这样,将
open("somefile.py")
更改为open("somefile.py", encoding="utf-8")
将显式指定编码,覆盖区域设置默认值,这将允许您正确地读入该值。对于惯用代码,您还需要使用
with
语句(以保证文件的确定性关闭),使其:相关问题 更多 >
编程相关推荐