python计算txt文本有多少单词

def count_words(filename):

    try:
        with open(filename) as file:
            contents = file.read()
    except FileNotFoundError:
        msg = 'Sorry, the file '+filename+' does not exist'
        print(msg)
    else:
        words = contents.split()
        n_words = len(words)
        print(n_words)

filenames = ['alice.txt','pi_digits.txt','hh.txt','little_women.txt','moby_dick.txt','siddhartha.txt']
for filename in filenames:
    count_words(filename)

结果如下

29461
3
Sorry, the file hh.txt does not exist
189079
215136
42172
>>> 

值得注意的是,如果文本中放的是数字,而不是字母(单词),则不需要用split()来分割,否则会出错,这是因为分割split()将一串数字看成一个字符引起的。如上面的pi_digits.txt的文本如下:

3.1415926535
  8979323846
  2643383279

分割之后

>>> with open('pi_digits.txt') as file:
    w = file.read()
    w.split()


['3.1415926535', '8979323846', '2643383279']

可见最开始给的结果3就是这样来的,看成了三个字符。
如果有数字文本,那么去掉split()函数就可以了,还是以pi为例:

def count_words(filename):

    try:
        with open(filename) as file:
            contents = file.read()
    except FileNotFoundError:
        msg = 'Sorry, the file '+filename+' does not exist'
        print(msg)
    else:
        n_words = len(contents)
        print(n_words)

filename = 'pi_digits.txt'
count_words(filename)

结果如下:

38
>>> 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值