#!/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()
|
1 answer
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
|
format
? And why, using the third python, you write# -*- coding: utf-8 -*-
andprint
without brackets? And yet, your file never closes, sincef.close()
afterreturn
. - Flowneeeu
, try to run in 3.5 - titov_andrei