python文本处理
某txt文件保存了一些数据,读取该文件的特定数据将数据返回为一个列表。
比如文本
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! 95 96 92 92 92 90 95 92 90 90 100 90 95 92 90 88 95 96 100 92 95 94 100 88 100 86 95 94 95 88 100 94 100 94 95 96 95 96 95 94 90 92 92 96 92 96 90 90 90 88 92 96 100 92
取出里面的数字并统计
#!/usr/bin/env python import collections import re fp = open(r'C:\Users\test\this.txt','r') word = re.findall(r'\d+',fp.read().lower()) print word print collections.Counter(word)
结果
['95', '96', '92', '92', '92', '90', '95', '92', '90', '90', '100', '90', '95', '92', '90', '88', '95', '96', '100', '92', '95', '94', '100', '88', '100', '86', '95', '94', '95', '88', '100', '94', '100', '94', '95', '96', '95', '96', '95', '94', '90', '92', '92', '96', '92', '96', '90', '90', '90', '88', '92', '96', '100', '92'] Counter({'92': 11, '95': 10, '90': 9, '100': 7, '96': 7, '94': 5, '88': 4, '86': 1})
变亦不变
浙公网安备 33010602011771号