2016-03-03 49 views
0

我有一個Python 3程序,讀取從Windows 1252編碼文件某些字符串:打印與編碼到標準輸出,在Python 3

with open(file, 'r', encoding="cp1252") as file_with_strings: 
    # save some strings 

這是我後來想寫入標準輸出。我試過這樣做:

print(some_string) 
# => UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 180: ordinal not in range(128) 

print(some_string.decode("utf-8")) 
# => AttributeError: 'str' object has no attribute 'decode' 

sys.stdout.buffer.write(some_str) 
# => TypeError: 'str' does not support the buffer interface 

print(some_string.encode("cp1252").decode("utf-8")) 
# => UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 180: invalid continuation byte 

print(some_string.encode("cp1252")) 
# => has the unfortunate result of printing b'<my string>' instead of just the string 

我在這裏撓我的腦袋。我想在cp1252中打印出我從文件中得到的字符串。 (在我的終端中,當我做more $file時,這些字符顯示爲問號,所以我的終端可能是ascii。)

想了解一下!謝謝!

+0

什麼'string_to_print = some_string.decode( 'UTF-8'); print(string_to_print)'do? – hd1

+0

這只是一個str,所以我得到'AttributeError:'str'對象沒有屬性'decode'' –

+0

「(在我的終端中,當我多做$文件時,這些字符顯示爲問號,所以我的終端可能是ascii 。)「< - 不,看起來好像在你的答案中寫着cp1252,那麼你的終端編碼可能與你的語言環境不匹配。 –

回答

1

爲了同樣的問題沒有人在那裏,我落得這樣做:

to_print = (some_string + "\n").encode("cp1252") 
sys.stdout.buffer.write(to_print) 
sys.stdout.flush() # I write a ton of these strings, and segfaulted without flushing 
1

When you encode with cp1252, you have to decode with the same.

如:

import sys 
txt = ("hi hello\n").encode("cp1252") 
#print((txt).decode("cp1252")) 
sys.stdout.buffer.write(txt) 
sys.stdout.flush() 

這將打印 「嗨,你好\ N」(這是編碼在解碼之後,在cp1252中)。

+1

「decode」只是試圖打印一個Unicode字符串,然後打印,這會將您引導回到開始位置。您的示例僅適用,因爲它只包含ASCII字符。 –

+0

是的,同意了。必須使用緩衝區編寫器。 –

+0

這對我有很大的幫助。我正在從STDIN中讀取數據,並寫入文件,因爲您可以在open()中設置編碼,但打印是一場噩夢。 –

0

您要麼滾動到您的腳本或您的區域設置已損壞。您應該修復您的環境,而不是將腳本修復到您的環境中,因爲這會使腳本非常脆弱。

如果你是管道系統,Python假定輸出應該是「ASCII」,並將stdout的編碼設置爲「ASCII」。

在正常情況下,Python使用locale來計算應用於stdout的編碼。如果您的語言環境中斷(未安裝或損壞),Python將默認爲「ASCII」。 「C」的語言環境也會給你一個「ASCII」的編碼。

通過輸入locale來檢查您的語言環境,並確保沒有錯誤返回。例如。

$ locale 
LANG="en_GB.UTF-8" 
LC_COLLATE="en_GB.UTF-8" 
LC_CTYPE="en_GB.UTF-8" 
LC_MESSAGES="en_GB.UTF-8" 
LC_MONETARY="en_GB.UTF-8" 
LC_NUMERIC="en_GB.UTF-8" 
LC_TIME="en_GB.UTF-8" 
LC_ALL= 

如果一切都失敗了,或者你管,你可以通過設置PYTHONIOENCODING環境變量覆蓋Python的區域設置檢測。例如。

$ PYTHONIOENCODING=utf-8 ./my_python.sh 

請記住,外殼具有一個語言環境和終端具有編碼 - 它們都需要被正確地設置

+0

沒有管道,但它也不是我的環境 - 這是一個程序,我必須在學校服務器上運行,它有ascii終端。我可以改變我的個人環境或使用不同的終端,但我不能保證分級人員會。 –

+0

這是Debian,我正在交付一個.py文件,這個文件將被另一臺計算機上的某個人用python3運行,但是從相同的文件中讀取,並且總是試圖寫入ascii stdout –

+0

如果你的終端真的是ASCII碼(他們可能不是),爲什麼你的答案編碼爲「cp1252」? –

相關問題