fork download
  1. from collections import Counter
  2. import chardet
  3.  
  4.  
  5. def check_encoding(fname):
  6. rawdata = open(fname, "rb").read()
  7. result = chardet.detect(rawdata)
  8. return result['encoding']
  9.  
  10. def top_10(f):
  11. array = [line.strip().split() for line in f]
  12. data = []
  13. m = ()
  14. for i in range(len(array)):
  15. for j in range(len(array[i])):
  16. if len(array[i][j]) > 6:
  17. data.append(array[i][j])
  18. p = Counter(data)
  19. m = (p.most_common(10))
  20. return m
  21.  
  22.  
  23. news = ["newsafr.txt", "newscy.txt", "newsfr.txt", "newsit.txt"]
  24. ecod = []
  25. for enc in news:
  26. ecod.append(check_encoding(enc))
  27. news_dict = {"newsafr.txt":"utf-8",
  28. "newscy.txt":"ascii",
  29. "newsfr.txt":"ISO-8859-5",
  30. "newsit.txt":"windows-1251"
  31. }
  32.  
  33.  
  34.  
  35. with open("newsafr.txt", "r", encoding="utf-8") as f:
  36. print(*(top_10(f)))
  37. with open("newscy.txt", "r", encoding="ascii") as f:
  38. print(*(top_10(f)))
  39. with open("newsfr.txt", "r", encoding="ISO-8859-5") as f:
  40. print(*(top_10(f)))
  41. with open("newsit.txt", "r", encoding="windows-1251") as f:
  42. print(*(top_10(f)))
  43.  
  44.  
  45.  
  46.  
Runtime error #stdin #stdout #stderr 0.02s 27712KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 26, in <module>
  File "./prog.py", line 6, in check_encoding
FileNotFoundError: [Errno 2] No such file or directory: 'newsafr.txt'