如何检查字符串是unicode还是ascii? [英] How do I check if a string is unicode or ascii?
本文介绍了如何检查字符串是unicode还是ascii?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
在Python 3中,所有字符串都是序列的Unicode字符。有一个字节
类型保存原始字节。
在Python 2中,一个字符串可能是 str
或类型为 unicode
。你可以知道使用这样的代码:
def whatisthis(s):
if isinstance(s,str )
打印普通字符串
elif isinstance(s,unicode):
printunicode string
else:
打印不是字符串
What do I have to do in Python to figure out which encoding?
解决方案
In Python 3, all strings are sequences of Unicode characters. There is a bytes
type that holds raw bytes.
In Python 2, a string may be of type str
or of type unicode
. You can tell which using code something like this:
def whatisthis(s):
if isinstance(s, str):
print "ordinary string"
elif isinstance(s, unicode):
print "unicode string"
else:
print "not a string"
这篇关于如何检查字符串是unicode还是ascii?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文