admin管理员组

文章数量:1432613

My file setting in VS Code is set to UTF-8 (default) rendering. If I run my line via Shift+Enter, the python Native REPL opens but i get the wrong encoding as i show below. However, If i run using the interactive window (jupyter) or via the terminal I get the correct rendering.

example code:

print('Accented letters: á, é, í, ó, ú, ü, ñ')

I get this wrong output as follows:

Accented letters: á, é, í, ó, ú, ü, ñ

My file setting in VS Code is set to UTF-8 (default) rendering. If I run my line via Shift+Enter, the python Native REPL opens but i get the wrong encoding as i show below. However, If i run using the interactive window (jupyter) or via the terminal I get the correct rendering.

example code:

print('Accented letters: á, é, í, ó, ú, ü, ñ')

I get this wrong output as follows:

Accented letters: á, é, í, ó, ú, ü, ñ
Share Improve this question edited Nov 20, 2024 at 23:15 Ash asked Nov 18, 2024 at 20:18 AshAsh 315 bronze badges 6
  • I can't reproduce this; both .ipynb files and the # %% cell notation are working fine for me. Can you provide more debugging steps? – Anerdw Commented Nov 19, 2024 at 0:08
  • Not sure how i can oprovide more details. i'm running the code snippets using shift enter, which opens the vscode interactive python repl. String outputs there are scrambled, obviously encoding problem. – Ash Commented Nov 19, 2024 at 4:17
  • Ok so aparently i'm running lines in python REPL via shift+enter. I If you right click at a line, you can select run python-> run line in PYthon REPL (which looks exatcly like the interactive window if you right click then select run in interactive window). If i run in interactive window, the output is encoded correctly. But what is the difference between the interactive window and the python REPL? they both look and behave the same apart from the encoding problem. and why do i get an enconding problem running via Python REPL? – Ash Commented Nov 19, 2024 at 4:51
  • Consider writing an edit to address those issues. Don't write a big "EDIT:" section, but do update your questions with those details since they'll be relevant to any good answer. – Anerdw Commented Nov 19, 2024 at 5:16
  • I'm assuming you are a Windows user? It isn't a question of whether the Python REPL and the IPython interactive window are doing anything different. The question is what encoding the console/terminal that REPL is running in. You are using utf-8 in Python but your IO boundary in the terminal is not UTF-8. – Andj Commented Nov 19, 2024 at 20:30
 |  Show 1 more comment

1 Answer 1

Reset to default 0

set the REPL encoding to utf-8?

The code below works for me:

print(('Accented letters: á, é, í, ó, ú, ü, ñ').encode('cp1252').decode('utf-8'))

本文标签: Why does VS Codes39s Python REPL window output letters with wrong encodingStack Overflow