简体   繁体   中英

Error (unicode error) 'utf-8' codec can't decode byte - code = compile(f.read(), fname, 'exec')

I'm new with python. I'm trying to run this code:

llaves=("España","Francia","Inglaterra")
dicPaises={llaves[0]:"Madrid",llaves[1]:"Paris",llaves[2]:"Londres"}
print(dicPaises)

the result is the following error:

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).

I'm using visual studio and python 3.9. I already tried with # - - coding: utf-8 - - But it does not work

By the looks of things, the compiler is having problem with the 'ñ', because that's the character that it would probably struggle with, and it's at position 4, which is the one highlighted by the error message . See this answer for a similar solution:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 2: invalid continuation byte

It seems like your encoding has got mixed up. \xf1 - the byte encoding raised in the error - corresponds to 'ñ' with latin1 encoding:

>>>"España".encode("latin1")
b'Espa\xf1a'
>>>b'\xf1'.decode('latin1')
'ñ'

Whereas utf-8 wants to encode the ñ as a combination of a '~' and an 'n':

>>>b'\xc3\xb1a'.decode('utf-8')
'ñ'

As @JosefZ points out, you should use utf-8 encoding always, and instead tell VS Code to use this instead of latin1 . I think you can find the options you need here:

https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding?view=powershell-7.2

if you ctrl+f for "Configuring VS Code" you should find the info you need.

Thanks to everyone for your replies. I fixed it finally by changing the local settings in language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM