0
1 import sys
2 import string
3 from collections import Counter
4
5 def count_words(input_file_path, *w_f):
6 tab = dict.fromkeys([ord(i) for i in string.punctuation], u' ')
7 words = []
8 count = 0
9 frequency = Counter()
10 with open(input_file_path, "r") as fp:
11 for line in fp.readlines():
12 linei = line.translate(tab)
13 words = linei.split()
14 count += len(words)
15 for item in words:
16 frequency[item] += 1
17 if w_f:
18 with open(w_f, "w") as wfp:
19 to_write = fp.read()
20 wfp.write(to_write)
21
22 sorted(frequency.items())
23 print "total word count %d" % count
24 print "Frequency "
25 print frequency
26
27 if __name__ == '__main__':
28 if len(sys.argv) == 2:
29 count_words(sys.argv[1])
30 elif len(sys.argv) == 3:
31 count_words(sys.argv[1], sys.argv[2])
32 else: raise Exception("Insufficient Arguments")
私は単語数をカウントするプログラムを作成しました。 エラーはタイプエラーです。 それはtranslateがバッファオブジェクトを期待していると言います。私が推測するユニコードと関係があります。文字バッファオブジェクトの翻訳でエラーが発生しました
正確な問題は何ですか? documentation on str.translate
から
代わりに、私はちょうどdictを文字列儀に変換できますか? @Rawing –
ユニコード形式の場合、テーブルはマッピング儀式ですか? –