![](/img/trans.png)
[英]UnicodeEncodeError while redirecting output to file - python 2.7
[英]Redirecting python output to a file causes UnicodeEncodeError on Windows
我正在嘗試將python腳本的輸出重定向到文件。 當輸出包含非ASCII字符時,它可以在macOS和Linux上運行,但不能在Windows上運行。
我把這個問題推導出來了一個簡單的測試。 以下是Windows命令提示符窗口中顯示的內容。 該測試僅是一次打印呼叫。
Microsoft Windows [Version 10.0.17134.472]
(c) 2018 Microsoft Corporation. All rights reserved.
D:\>set PY
PYTHONIOENCODING=utf-8
D:\>type pipetest.py
print('\u0422\u0435\u0441\u0442')
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
D:\>type test.txt
Тест
D:\>type test.txt | iconv -f utf-8 -t utf-8
Тест
D:\>set PYTHONIOENCODING=
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
Traceback (most recent call last):
File "pipetest.py", line 1, in <module>
print('\u0422\u0435\u0441\u0442')
File "C:\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
D:\>python -V
Python 3.7.2
可以看到設置PYTHONIOENCODING環境變量會有所幫助,但我不明白為什么需要設置它。 當輸出是終端時,它可以工作,但是如果輸出是文件,則失敗。 當stdout不是控制台時,為什么要使用cp1252?
也許這是一個錯誤,可以在Windows版本的python中修復嗎?
我正在嘗試將python腳本的輸出重定向到文件。 當輸出包含非ASCII字符時,它可以在macOS和Linux上運行,但不能在Windows上運行。
我把這個問題推導出來了一個簡單的測試。 以下是Windows命令提示符窗口中顯示的內容。 該測試僅是一次打印呼叫。
Microsoft Windows [Version 10.0.17134.472]
(c) 2018 Microsoft Corporation. All rights reserved.
D:\>set PY
PYTHONIOENCODING=utf-8
D:\>type pipetest.py
print('\u0422\u0435\u0441\u0442')
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
D:\>type test.txt
Тест
D:\>type test.txt | iconv -f utf-8 -t utf-8
Тест
D:\>set PYTHONIOENCODING=
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
Traceback (most recent call last):
File "pipetest.py", line 1, in <module>
print('\u0422\u0435\u0441\u0442')
File "C:\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
D:\>python -V
Python 3.7.2
可以看到設置PYTHONIOENCODING環境變量會有所幫助,但我不明白為什么需要設置它。 當輸出是終端時,它可以工作,但是如果輸出是文件,則失敗。 當stdout不是控制台時,為什么要使用cp1252?
也許這是一個錯誤,可以在Windows版本的python中修復嗎?
我正在嘗試將python腳本的輸出重定向到文件。 當輸出包含非ASCII字符時,它可以在macOS和Linux上運行,但不能在Windows上運行。
我把這個問題推導出來了一個簡單的測試。 以下是Windows命令提示符窗口中顯示的內容。 該測試僅是一次打印呼叫。
Microsoft Windows [Version 10.0.17134.472]
(c) 2018 Microsoft Corporation. All rights reserved.
D:\>set PY
PYTHONIOENCODING=utf-8
D:\>type pipetest.py
print('\u0422\u0435\u0441\u0442')
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
D:\>type test.txt
Тест
D:\>type test.txt | iconv -f utf-8 -t utf-8
Тест
D:\>set PYTHONIOENCODING=
D:\>python pipetest.py
Тест
D:\>python pipetest.py > test.txt
Traceback (most recent call last):
File "pipetest.py", line 1, in <module>
print('\u0422\u0435\u0441\u0442')
File "C:\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
D:\>python -V
Python 3.7.2
可以看到設置PYTHONIOENCODING環境變量會有所幫助,但我不明白為什么需要設置它。 當輸出是終端時,它可以工作,但是如果輸出是文件,則失敗。 當stdout不是控制台時,為什么要使用cp1252?
也許這是一個錯誤,可以在Windows版本的python中修復嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.