首页 > Python:统计字数(中英文)

Python:统计字数(中英文)

需求:统计给定文本的字数:包括中英文
本来是这样写的,但是发现行不通,
代码:

def GetWordNums(text):
    num = 0
    for i in text:
        if i not in ' \n!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
            num = num +1
    return num

希望大家帮忙指点下
以下方法可以使用:

len("测试".encode("gbk").decode("gbk"))

我是用Python 2.7的,测试前,将其decode('gbk')即可。
Python对中文的支持度一直都不太好,我也是初学者,有空常交流。

GetWordsNum(text.decode('gbk'))


快使用Python 3,哼哼哈兮~

; python3 
Python 3.2.3 (default, Feb 20 2013, 17:02:41) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('我很high')
6
【热门文章】
【热门文章】