2

I am coding python3 in vscode.i install code-runner in vscode and use the plugin to run my python code.But when i wanted to print some utf8 string,there is the error.

[Running] python "/develop/python/secondLesson.py" Traceback (most recent call last): File "/develop/python/secondLesson.py", line 2, in print('\u5475\u5475') UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

[Done] exited with code=1 in 0.049 seconds

My file is utf8,and when i run the code in my terminal , my code is OK.

N.Deng
  • 39
  • 3
  • 1
    You might want to try the Python Extension (https://marketplace.visualstudio.com/items?itemName=ms-python.python) for VS Code and then use the command from the Command Palette `Run Python File In Terminal`. That should work. – Don Mar 18 '18 at 05:07

1 Answers1

0

Try using your_string.encode('utf-8'). Using your example:

print('\u5475\u5475'.encode('utf-8'))

Here's another answer explaning this in more detail.

ConorSheehan1
  • 1,640
  • 1
  • 18
  • 30
  • 1
    thank you,you let me learn more about the encode of python.I add the `encode('utf-8')` and there is no error,but the console output the utf-8 in ascii: **b'\xe5\x91\xb5\xe5\x91\xb5'** at last ,i found the a same issue on github [link](https://github.com/formulahendry/vscode-code-runner/issues/25) .So it is seem a bug of the plugin of vscode. Anyway thank you to answer my question. – N.Deng Mar 12 '18 at 02:24