2016-03-03 86 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”? –

相关问题