《Python面试题》 练习二

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_37189082/article/details/100540582

1.统计 txt文件中每个单词使用的次数

import collections
import re
 
 
def word_cal():
 
    with open('test.txt','r') as fp:
        content = re.split('[ .,]', fp.read())
    b = collections.Counter(content)
    with open('test2.txt','w') as result_file:
        for key,value in b.items():
            result_file.write(key+':'+str(value)+'\n')
 
def main():
    word_cal()
 
if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/qq_37189082/article/details/100540582