简体   繁体   English

python3打印函数发出'ascii'编解码器无法编码字符错误

[英]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-8PYTHONIOENCODING=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.

相关问题 / notifications / ajax /'ascii'编解码器处的UnicodeEncodeError无法编码字符:序数不在范围内(128) - UnicodeEncodeError at /notifications/ajax/ 'ascii' codec can't encode character: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器...在尝试编码JavaScript的结构时 - UnicodeDecodeError: 'ascii' codec… when trying to encode a structure for JavaScript Javascript 在 USB 流中编码“ENQ”ASCII 字符 - Javascript Encode "ENQ" ASCII Character in USB Stream Internet Explorer 8没有对该字符进行编码,该怎么办? - internet explorer 8 didn't encode the character, how can I do? 未捕获的错误:无法对对象进行形式编码 - Uncaught Error: Can't form encode an Object ASCII码到PHP中的字符错误 - ASCII Number to Character Error in PHP 无法打印错误消息 - Can't print error message 我有一个用于ascii密码的编码功能,但需要如何解码的帮助 - I have the encode function for an ascii cipher, but need help to how to decode it Python中Javascript中的ASCII返回字符显示为换行符和“未终止的字符串错误” - ASCII return character within Javascript within Python shows up as a line feed and “unterminated string error” 打印MySQL SUM()函数的json_encode - print json_encode of a MySQL SUM() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM