sub_string.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #coding=utf-8
  2. import os,sys
  3. import re
  4. #从输入参数获取文件路径
  5. path = raw_input("input word file directory:")
  6. #获得目录下所有文件列表
  7. def getFileList(dir,fileList):
  8. if os.path.isfile(dir):
  9. fileList.append(dir.decode('gbk'))
  10. elif os.path.isdir(dir):
  11. for s in os.listdir(dir):
  12. newDir=os.path.join(dir,s)
  13. getFileList(newDir, fileList)
  14. return fileList
  15. def loadText(file,arrStr):
  16. file_object = open(file,'rb')
  17. dict={}
  18. for line in file_object:
  19. strs=re.findall(r'[A-Za-z]+',line)
  20. #strs=re.findall(r'[A-Za-z0-9_]+',line)
  21. # print("读:",strs)
  22. for str in strs:
  23. if str.find("j") !=-1 and str.find("n") !=-1 and str.find("m") !=-1 :
  24. arrStr[str] = 1
  25. if dict.has_key(str):
  26. dict[str]+=1
  27. else:
  28. dict[str]=1
  29. result=sorted(dict.items(),key=lambda k:k[1],reverse=True)
  30. file_object.close()
  31. # print(result)
  32. return result
  33. arrStr = {}
  34. arrCount = {}
  35. for file in getFileList(path,[]):
  36. arrCount.update( loadText(file,arrStr) )
  37. str1 = ""
  38. for text in arrStr:
  39. str1 +=" " + text
  40. # f = open(path+"/words.txt",'w') #若文件不存在,系统自动创建。'a'
  41. # f.write(str1) #将字符串写入文件中
  42. # f.close() #关闭
  43. print(str1)
  44. raw_input("end...")