Tôi gặp sự cố liên quan đến Unicode trong Python. Tôi có thể in tiền phạt đầu ra trong một thiết bị đầu cuối thông thường, nhưng nếu tôi chuyển hướng stdout
nơi khác (hoặc chụp nó với các mô-đun subprocess
), tôi nhận được một UnicodeEncodeError
:UnicodeEncodeError khi chuyển hướng stdout
$ cat example.py
print u'Example: \u00F1'
$ python example.py
Example: ñ
$ python example.py > /dev/null
Traceback (most recent call last):
File "example.py", line 1, in <module>
print u'Example: \u00F1'
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 9: ordinal not in range(128)
Tại sao điều này? Làm thế nào tôi có thể sửa chữa nó?
http://stackoverflow.com/questions/492483/setting-the-correct-encoding-when-piping-stdout-in-python về cơ bản là cùng một câu hỏi, câu trả lời đã giải quyết được vấn đề tôi gặp phải (hy vọng). – dbr