[英]python3 print function emits 'ascii' codec can't encode character error
A user inputs a string on my website. 用户在我的网站上输入一个字符串。 They input a non-ascii character.
他们输入一个非ascii字符。
The javascript saves their input, packages it with JSON.stringify(), and sends it to the server. javascript保存输入,用JSON.stringify()打包,并将其发送到服务器。
The server, running Python3, unpacks the JSON with json.loads and saves this string in a Node object, then runs the line 运行Python3的服务器使用json.loads解压缩JSON并将此字符串保存在Node对象中,然后运行该行
print('looks like {}'.format(node_obj))
I receive the error 我收到错误
'ascii' codec can't encode character error '\u2212' in position 941: ordinal not in range(128)
It seems to me that the print function in Python3 is trying to convert the unicode string to ascii! 在我看来,Python3中的print函数试图将unicode字符串转换为ascii! (convert to bytes object using ascii encoding?)
(使用ascii编码转换为bytes对象?)
Is it possible that my FreeBSD server does not support UTF-8, causing Python's print function to make this conversion? 我的FreeBSD服务器可能不支持UTF-8,导致Python的打印功能进行转换吗? Or perhaps the string was never properly sanitized in the first place, and I should be doing that in the javascript when I first receive it from the user?
或者字符串从来没有在第一时间正确消毒,我应该在第一次从用户那里收到它时在javascript中这样做?
Let me know what further information is useful to you. 让我知道更多信息对您有用。
What does the locale
command say? locale
命令说什么?
You can make Python use utf-8 with either LANG=en_US.UTF-8
or PYTHONIOENCODING=utf-8
. 你可以让Python使用带有
LANG=en_US.UTF-8
或PYTHONIOENCODING=utf-8
。
Setting LANG in the default environment is platform-dependent: https://unix.stackexchange.com/questions/342817/how-do-i-add-a-language-in-freebsd 在默认环境中设置LANG取决于平台: https : //unix.stackexchange.com/questions/342817/how-do-i-add-a-language-in-freebsd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.