我刚接触蟒蛇。我试着运行这段代码:
llaves=("España","Francia","Inglaterra")
dicPaises={llaves[0]:"Madrid",llaves[1]:"Paris",llaves[2]:"Londres"}
print(dicPaises)
结果是以下错误:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py", line 285, in run_file
The thread 0x1 has exited with code 0 (0x0).
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 267, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\runpy.py", line 242, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
File "C:\Users\JANSIR\source\repos\Pruebas\Pruebas.py", line 8
llaves=("España","Francia","Inglaterra")
^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf1 in position 4: invalid continuation byte
The program 'python.exe' has exited with code 1 (0x1).
我正在使用visual和python3.9。我已经尝试过# --编码: utf-8 -- ,但是它不起作用,
发布于 2022-07-26 15:08:19
从外观上看,编译器对“i”有问题,因为这是它可能会处理的字符,而则位于位置4,这是错误消息突出显示的一个字符。有关类似的解决方案,请参见此答案:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 2: invalid continuation byte
你的编码好像搞混了。\xf1 1--错误中引发的字节编码--对应于“latin1”编码:
>>>"España".encode("latin1")
b'Espa\xf1a'
>>>b'\xf1'.decode('latin1')
'ñ'
而utf-8则希望编码“~”和“n”的组合:
>>>b'\xc3\xb1a'.decode('utf-8')
'ñ'
正如@JosefZ所指出的,您应该始终使用utf-8编码,并告诉VS代码使用这个编码而不是latin1。我认为你可以在这里找到你需要的选择:
如果你ctrl+f的“配置VS代码”,你应该找到你需要的信息。
发布于 2022-07-27 14:24:44
感谢大家的回复。最后,我通过更改语言中的本地设置来修复它。
https://stackoverflow.com/questions/73125063
复制相似问题