Skip to content

Instantly share code, notes, and snippets.

@likangwei
Created April 16, 2015 14:48
Show Gist options
  • Save likangwei/fddeaf7ba5f4a962062e to your computer and use it in GitHub Desktop.
Save likangwei/fddeaf7ba5f4a962062e to your computer and use it in GitHub Desktop.
背单词小case
import os
doc_dir = r"D:\code\python\django\docs"
from path import Path
dd = Path(doc_dir)
import operator
result = {}
import re
p = re.compile(r'[a-zA-Z]+')
for txt_file in dd.walk("*.txt"):
for line in open(txt_file).readlines():
for word in p.findall(line):
if operator.contains(result, word):
result[word] = result[word] + 1
else:
result[word] = 1
# print result
sortList = sorted(result.items(), key=lambda d: d[1], reverse=True)
with open("result.txt", 'wb') as result_file:
for str,count in sortList:
result_file.write('%s %s\n' %(str, count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment