#!/usr/bin/python2.7 # -*- coding: utf-8 -*- def print_words(): import string f = open('text.txt', 'r') # чтение из текст файла f.seek(0) str = f.read().split() my_d = dict() my_set =set() for i in str: # пропускаю через множества my_set.add(i) for word in my_set: new_word = word.lower() new_word1 = new_word.strip(string.punctuation) # удаление знаков припенания for ky in new_word1: if ky not in my_d: my_d[ky] = 1 else: my_d[ky] += 1 for y in my_d: print (y,'-',my_d[y]) # Вывод на экран f.close() return print_words() 
  • Correct the code and describe the task in detail, as well as indicate what and where does not work. What does the current form of code in general format ? And why, using the third python, you write # -*- coding: utf-8 -*- and print without brackets? And yet, your file never closes, since f.close() after return . - Flowneee
  • ('\ x81', '-', 70) ('\ x80', '-', 85) ('\ x83', '-', 27) ('\ x82', '-', 103) - Paul Silber
  • in 2.7 you need to specify u , try to run in 3.5 - titov_andrei
  • print (y, '-', my_d [y]) # u on this line? u'y 'something doesn't work ... (I only have version 2.7) We only learn from it for now ... - Paul Silber

1 answer 1

print (y,'-',my_d[y]) replace with print '%s - %d' % (y.decode('utf-8'), my_d[y])

  • - 70 - 85 - 27 - 103 Already better but still very bad :) - Paul Silber
  • What encoding is the data in text.txt? - Sergey Gornostaev
  • print '{0} - {1}'. format (y, my_d [y]) like this, same thing. - Paul Silber
  • Text in Russian. - Paul Silber
  • Russian is understandable, but what is the encoding? utf-8, cp866, cp1251, koi8-r? - Sergey Gornostaev