• 技术文章 >后端开发 >Python教程

    Python实现统计英文单词个数及字符串分割代码

    2016-06-06 11:17:43原创1933
    字符串分割

    代码如下:


    str="a|and|hello|||ab"
    alist = str.split('|')
    print alist

    结果


    代码如下:


    str="a hello{这里换成5个空格}world{这里换成3个空格}"
    alist=str.split(' ')
    print alist


    统计英文单词的个数的python代码

    代码如下:


    # -*- coding: utf-8 -*-
    import os,sys
    info = os.getcwd() #获取当前文件名称
    fin = open(u'c:/a.txt')

    info = fin.read()
    alist = info.split(' ') # 将文章按照空格划分开

    fout = open(u'c:/count.txt', 'w')
    fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
    ##fout.write('%s' % alist)
    fout.close()

    allen = len(alist) # 总的单词数
    nulen = alist.count('') # 空格的数量
    print "words' number is",allen
    print "null number is",nulen
    print "poor words number is", allen-nulen # 实际的单词数目

    fin.close()

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:Python 26进制计算实现方法 下一篇:Python定时执行之Timer用法示例
    千万级数据并发解决方案

    相关文章推荐

    • python列表详解(总结分享)• 详细了解Python进程池与进程锁• 一起聊聊Python的编码样式• 带你搞懂Python反序列化• 归纳整理!Python实用技巧总结
    1/1

    PHP中文网