2
votes

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.

1
You might want to try the Python Extension (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

1 Answers

0
votes

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.