繁体   English   中英

为什么python IDLE和Console会产生不同的结果

[英]Why python IDLE and Console produce different results

我写了一个简单的Python脚本来将中文标点符号翻译成英文。

import codecs, sys

def trcn():
    tr = lambda x: x.translate(str.maketrans(""",。!?;:、()【】『』「」﹁﹂“”‘’《》~¥…—×""", """,.!?;:,()[][][][]""''<>~$^-*"""))
    out = codecs.getwriter('utf-8')(sys.stdout)
    for line in sys.stdin:
        out.write(tr(line))

if __name__ == '__main__':
    if not len(sys.argv) == 1:
        print("usage:\n\t{0} STDIN STDOUT".format(sys.argv[0]))
        sys.exit(-1)
    trcn()
    sys.exit(0)

但是UNICODE出了点问题。 我无法通过它。 错误消息:

Traceback (most recent call last):
  File "trcn.py", line 13, in <module>
    trcn()
  File "trcn.py", line 7, in trcn
    out.write(tr(line))
  File "C:\Python31\Lib\codecs.py", line 356, in write
    self.stream.write(data)
TypeError: must be str, not bytes

之后,我在IDLE和Console中测试out.write() 他们产生了不同的结果 我不知道为什么。

在IDLE

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys,codecs
>>> out = codecs.getwriter('utf-8')(sys.stdout)
>>> out.write('hello')
hello
>>>

在控制台中

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys,codecs
>>> out = codecs.getwriter('utf-8')(sys.stdout)
>>> out.write('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python31\Lib\codecs.py", line 356, in write
    self.stream.write(data)
TypeError: must be str, not bytes
>>>

平台:Windows XP EN

您的编码输出以字节形式从编码器输出,因此必须传递给sys.stdout.buffer

out = codecs.getwriter('utf-8')(sys.stdout.buffer)

我不完全确定为什么你的代码在IDLE和控制台中的行为不同,但上面的内容可能有所帮助。 也许IDLE的sys.stdout实际上需要字节而不是字符(希望它有一个也需要字节的.buffer )。

IDLE将stdout重定向到自己的GUI输出。 它显然接受字节和字符串,正常的stdout不接受。

将其解码为Unicode,或将其打印到sys.stdout.buffer。

很明显控制台的编码不是utf-8。 在控制台中调用python时,有一种方法可以将编码指定为可选参数。 只需在python docs中查找它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM