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.
我在vscode.i中编码python3在vscode中安装code-runner并使用插件来运行我的python代码。但是当我想打印一些utf8字符串时,就会出现错误。
[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)
[running] python“/develop/python/secondLesson.py”Traceback(最近一次调用最后一次):文件“/develop/python/secondLesson.py”,第2行,打印('\ u5475 \ u5475')UnicodeEncodeError:' ascii'编解码器无法编码位置0-1中的字符:序数不在范围内(128)
[Done] exited with code=1 in 0.049 seconds
[完成]在0.049秒内退出代码= 1
My file is utf8,and when i run the code in my terminal , my code is OK.
我的文件是utf8,当我在终端中运行代码时,我的代码没问题。
1 个解决方案
#1
0
Try using your_string.encode('utf-8')
. Using your example:
尝试使用your_string.encode('utf-8')。使用你的例子:
print('\u5475\u5475'.encode('utf-8'))
Here's another answer explaning this in more detail.
这是另一个答案,更详细地解释了这一点。
#1
0
Try using your_string.encode('utf-8')
. Using your example:
尝试使用your_string.encode('utf-8')。使用你的例子:
print('\u5475\u5475'.encode('utf-8'))
Here's another answer explaning this in more detail.
这是另一个答案,更详细地解释了这一点。