本文介绍了Python 2.7:测试字符串中的字符是否都是中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码测试字符串中的字符是否都是中文字符.它适用于 Python 3 但不适用于 Python 2.7.我如何在 Python 2.7 中执行此操作?
The following code tests if characters in a string are all Chinese characters. It works for Python 3 but not for Python 2.7. How do I do it in Python 2.7?
for ch in name:
if ord(ch) < 0x4e00 or ord(ch) > 0x9fff:
return False
推荐答案
# byte str (you probably get from GAE)
In [1]: s = """Chinese (汉语/漢語 Hànyǔ or 中文 Zhōngwén) is a group of related
language varieties, several of which are not mutually intelligible,"""
# unicode str
In [2]: us = u"""Chinese (汉语/漢語 Hànyǔ or 中文 Zhōngwén) is a group of related
language varieties, several of which are not mutually intelligible,"""
# convert to unicode using str.decode('utf-8')
In [3]: print ''.join(c for c in s.decode('utf-8')
if u'\u4e00' <= c <= u'\u9fff')
汉语漢語中文
In [4]: print ''.join(c for c in us if u'\u4e00' <= c <= u'\u9fff')
汉语漢語中文
要确保所有字符都是中文,应该这样做:
To make sure all the characters are Chinese, something like this should do:
all(u'\u4e00' <= c <= u'\u9fff' for c in name.decode('utf-8'))
在您的 Python 应用程序中,在内部使用 unicode - 尽早解码 &编码延迟 - 创建一个 unicode 三明治.
In your python application, use unicode internally - decode early & encode late - creating a unicode sandwich.
这篇关于Python 2.7:测试字符串中的字符是否都是中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!