仅包含ASCII字符的UNICODE字符串是否总是等于ASCII字符串? [英] Will a UNICODE string just containing ASCII characters always be equal to the ASCII string?

查看:95
本文介绍了仅包含ASCII字符的UNICODE字符串是否总是等于ASCII字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到以下情况:

 >> u’abc’==‘abc’

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

这将始终为真或可以它可能取决于系统区域设置?
(似乎字符串在python 3中是unicode,例如这个问题,但字节数在2.x)

解决方案

Python 2使用ASCII编解码器在 unicode str 之间进行强制转换。是的,这总是



也就是说,除非您弄乱了Python安装并使用 sys.setdefaultencoding() 到更改默认值。您通常无法执行此操作,因为 sys.setdefaultencoding()函数在启动时已从模块中删除,但是有货物人们使用 reload(sys)恢复该功能并将默认编码更改为其他编码,以尝试解决隐式编码和解码问题。正是由于这个原因,这是一件愚蠢的事情。


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: e.g. this question, but bytes in 2.x)

解决方案

Python 2 coerces between unicode and str using the ASCII codec when comparing the two types. 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. 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. This is a dumb thing to do for precisely this reason.

这篇关于仅包含ASCII字符的UNICODE字符串是否总是等于ASCII字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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