Python:哪个编码用于处理sys.argv? [英] Python: Which encoding is used for processing sys.argv?

查看:439
本文介绍了Python:哪个编码用于处理sys.argv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中, sys.argv 的元素是什么编码?是否使用 sys.getdefaultencoding()编码?

In what encoding are the elements of sys.argv, in Python? are they encoded with the sys.getdefaultencoding() encoding?


sys.getdefaultencoding ():返回Unicode实现使用的当前默认字符串编码的名称。

sys.getdefaultencoding(): Return the name of the current default string encoding used by the Unicode implementation.

PS :正如在一些答案中所指出的那样, sys.stdin.encoding 确实会是一个更好的猜测。我希望看到一个确切的答案,但是,指向固体源!

PS: As pointed out in some of the answers, sys.stdin.encoding would indeed be a better guess. I would love to see a definitive answer to this question, though, with pointers to solid sources!

PPS :正如Wim指出的,Python 3通过在sys.argv(如果我理解正确)中放置 str 对象来解决这个问题。对于Python 2.x来说,这个问题仍然存在。在Unix下,LC_CTYPE环境变量似乎是正确的检查,不是吗? Windows应该做什么(所以sys.argv元素可以正确解释任何控制台)?

PPS: As Wim pointed out, Python 3 solves this issue by putting str objects in sys.argv (if I understand correctly). The question remains open for Python 2.x, though. Under Unix, the LC_CTYPE environment variable seems to be the correct thing to check, no? What should be done with Windows (so that sys.argv elements are correctly interpreted whatever the console)?

推荐答案

应该是什么完成与Windows(所以sys.argv元素是正确地解释任何控制台)?

"What should be done with Windows (so that sys.argv elements are correctly interpreted whatever the console)?"

对于Python 2.x,请参阅对issue2128的评论

For Python 2.x, see this comment on issue2128.

(请注意,编码对于原始的sys.argv是正确的,因为某些字符可能已经被破坏,没有足够的信息可以撤消;例如,如果ANSI代码页不能表示希腊字母,那么它将被'a'变换。 )

(Note that no encoding is correct for the original sys.argv, because some characters may have been mangled in ways that there is not enough information to undo; for example, if the ANSI codepage cannot represent Greek alpha then it will be mangled to 'a'.)

这篇关于Python:哪个编码用于处理sys.argv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆