MRJob を使用して、非常に基本的なワードカウントの例を実装しようとしています。すべてがASCII入力で正常に機能しますが、入力にキリル文字を混ぜると、出力として次のようなものが得られます
"\u043c\u0438\u0440" 1
"again!" 1
"hello" 2
"world" 1
私が理解している限り、上記の最初の行は、キリル文字の単語「мир」のエンコードされた単一の出現であり、サンプルの入力テキストに関して正しい結果です。MRコードはこちら
class MRWordCount(MRJob):
def mapper(self, key, line):
line = line.decode('cp1251').strip()
words = line.split()
for term in words:
yield term, 1
def reducer(self, term, howmany):
yield term, sum(howmany)
if __name__ == '__main__':
MRWordCount.run()
WindowsでPython 2.7とmrjob 0.4.2を使用しています。私の質問は次のとおりです。
a) キリル文字入力で読みやすいキリル文字出力を正しく生成するにはどうすればよいですか? b) この動作の根本的な原因は何ですか?それは python/MR バージョンによるものですか、それとも Windows 以外では異なる動作が予想されるものですか? 手がかりはありますか?
python -c "print u'мир'" の出力を再現しています
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\encodings\cp866.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>