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

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

问题描述

sys.argv 的元素在 Python 中采用什么编码?它们是否使用 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 通过将 str 对象放在 sys.argv 中解决了这个问题(如果我理解正确的话).不过,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.

(注意,no 编码对于原始 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天全站免登陆