我该怎么做:
>>> s = u'hello'
>>> isinstance(s,str)
False
但我想 isinstance
返回 True
这个 Unicode 编码的字符串。是否有 Unicode 字符串对象类型?
原文由 A.D 发布,翻译遵循 CC BY-SA 4.0 许可协议
我该怎么做:
>>> s = u'hello'
>>> isinstance(s,str)
False
但我想 isinstance
返回 True
这个 Unicode 编码的字符串。是否有 Unicode 字符串对象类型?
原文由 A.D 发布,翻译遵循 CC BY-SA 4.0 许可协议
是否有 Unicode 字符串对象类型?
是的,它叫做 unicode
:
>>> s = u'hello'
>>> isinstance(s, unicode)
True
>>>
请注意,在 Python 3.x 中,此类型已被删除,因为 所有字符串现在都是 Unicode 。
原文由 user2555451 发布,翻译遵循 CC BY-SA 3.0 许可协议
2 回答7.2k 阅读✓ 已解决
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答884 阅读✓ 已解决
测试
str
:或者,如果您必须处理字节串,请分别测试
bytes
:这两种类型是故意不可互换的; use explicit encoding (for
str
->bytes
) and decoding (bytes
->str
) to convert between the types.In Python 2 , where the modern Python 3
str
type is calledunicode
andstr
is the precursor of the Python 3bytes
type,您可以使用basestring
来测试 两者:basestring
仅在 Python 2 中可用,并且是str
和unicode
的抽象基类型。如果你 只想 测试
unicode
,那么明确地这样做: