简体   繁体   English

只包含ASCII字符的UNICODE字符串是否总是等于ASCII字符串?

[英]Will a UNICODE string just containing ASCII characters always be equal to the ASCII string?

I noticed the following holds: 我注意到以下情况:

>>> u'abc' == 'abc'
True
>>> 'abc' == u'abc'
True

Will this always be true or could it possibly depend on the system locale? 这总是正确的还是可能取决于系统区域设置? (It seems strings are unicode in python 3: eg this question , but bytes in 2.x) (似乎字符串在python 3中是unicode:例如这个问题 ,但2.x中的字节)

Python 2 coerces between unicode and str using the ASCII codec when comparing the two types. 在比较两种类型时,Python 2使用ASCII编解码器在unicodestr之间unicode So yes, this is always true. 所以是的,这总是如此。

That is to say, unless you mess up your Python installation and use sys.setdefaultencoding() to change that default. 也就是说,除非你弄乱你的Python安装并使用sys.setdefaultencoding()来改变那个默认值。 You cannot do that normally, because the sys.setdefaultencoding() function is deleted from the module at start-up time, but there is a Cargo Cult going around where people use reload(sys) to reinstate that function and change the default encoding to something else to try and fix implicit encoding and decoding problems. 你不能这样做,因为sys.setdefaultencoding()函数在启动时从模块中删除 ,但有一个Cargo Cult四处走动,人们使用reload(sys)恢复该函数并将默认编码更改为尝试修复隐式编码和解码问题的其他东西。 This is a dumb thing to do for precisely this reason. 正是出于这个原因,这是一件愚蠢的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM