Created
April 16, 2015 14:48
-
-
Save likangwei/fddeaf7ba5f4a962062e to your computer and use it in GitHub Desktop.
背单词小case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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