Search code examples
pythonvisual-studio-codeprintingutf-8text-files

Cant print letters such as å, ä, ö in PowerShell and when I do it prints, for example, ä


I'm on Windows 11 using VS Code for the coding thing with python and using PowerShell when I execute my code. I got a text file on swedish which I want to print but when I print the letter å, ö and ä it prints them like ä and other type of strange characters.

I have saved the text file as UTF-8 and my chcp is on 65001 which should be the universal option but it still doesn't work. It does actually work when I save the text file as ANSI but I want to have it as UTF-8 and make it work.


Solution

  • Vscode uses the integrated terminal embedded in the system, so the characters displayed by the terminal are related to many aspects. System language, terminal code page, file encoding, etc. Any configuration inconsistency may cause your characters to display incorrectly.

    • As far as my machine is concerned, it is now the system Chinese language, the code page is 437, and the characters are displayed normally.

      enter image description here

    • In addition, you can also install Code Runner, by default it will output the result in the OUTPUT panel, it may bring you the correct display effect.

      enter image description here

      Please do not use Code Runner if there is no special requirement. Python works better as an official extension.

    • Configure the following launch.json, use Run Without Debugging, and the result is output in the DEBUG CONSOLE panel

      {
          "version": "0.2.0",
          "configurations": [
              {
                  "name": "Python: Current File",
                  "type": "python",
                  "request": "launch",
                  "program": "${file}",
                  "console": "internalConsole",
                  "justMyCode": true
              }
          ]
      }
      

      enter image description here