英文:
Why these python print commands behave differently with Python 2 and Python 3
问题 {#heading}
I noticed
$ python2 -c 'print("A"*20 + "\xef\xbe\xad\xde" + "A"*4)'
AAAAAAAAAAAAAAAAAAAAᆳ▒AAAA
and
$ python3 -c 'print("A"*20 + "\xef\xbe\xad\xde" + "A"*4)'
AAAAAAAAAAAAAAAAAAAAï¾­ÞAAAA
give different output.
I'm expecting to have the same string output with Python3 as with Python2.
I tried to change from string to bytes using
$ python3 -c 'print(b"A"*20 + b"\xef\xbe\xad\xde" + b"A"*4)'
b'AAAAAAAAAAAAAAAAAAAA\xef\xbe\xad\xdeAAAA'
but b'AAAAAAAAAAAAAAAAAAAA\xef\xbe\xad\xdeAAAA'
is not AAAAAAAAAAAAAAAAAAAAᆳ▒AAAA
.
i.e., when piped to another process, it's different.
Any idea how I can use Python3 to have the same output as with Python2? 英文:
I noticed
$ python2 -c 'print("A"*20 + "\xef\xbe\xad\xde" + "A"*4)'
AAAAAAAAAAAAAAAAAAAAᆳ▒AAAA
and
$ python3 -c 'print("A"*20 + "\xef\xbe\xad\xde" + "A"*4)'
AAAAAAAAAAAAAAAAAAAAï¾­ÞAAAA
gives different output.
I'm expecting to have same string output with python3 as with python2
I tried to change from string to bytes using
$ python3 -c 'print(b"A"*20 + b"\xef\xbe\xad\xde" + b"A"*4)'
b'AAAAAAAAAAAAAAAAAAAA\xef\xbe\xad\xdeAAAA'
but b'AAAAAAAAAAAAAAAAAAAA\xef\xbe\xad\xdeAAAA'
is not AAAAAAAAAAAAAAAAAAAAᆳ▒AAAA
ie when piped to another process it's different.
Any idea how I can use Python3 to have same output than with Python 2 ?
答案1 {#1}
得分: 1
Python2将字节直接发送到控制台而不进行解释。Python3在字符串中使用Unicode代码点而不是字节,并使用UTF-8编码将它们写出。
在Python2中,您可以获得相同的结果:
print("A"*20 + "\xef\xbe\xad\xde" + "A"*4).decode('mbcs').encode('utf-8')
英文:
Python2 is sending the bytes directly to your console without interpretation. Python3 is using Unicode codepoints in the string instead of bytes, and uses UTF-8 encoding to write them out.
You can get the same results in Python2:
print("A"*20 + "\xef\xbe\xad\xde" + "A"*4).decode('mbcs').encode('utf-8')
答案2 {#2}
得分: 1
An alternative is to use bytes
objects (which would be the equivalent of Python 2 str
), and to write to the standard out directly (to the raw buffer, not the text i/o wrapper):
>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
>>> sys.stdout.buffer
<_io.BufferedWriter name='<stdout>'>
>>> stdout_raw = sys.stdout.buffer
>>> data = b"A"*20 + b"\xef\xbe\xad\xde" + b"A"*4 # notice I'm using bytes literals
>>> stdout_raw.write(data)
AAAAAAAAAAAAAAAAAAAAᆳ�AAAA28
英文:
An alternative is to use bytes
objects (which would be the equivalent of Python 2 str
), and to write to the standard out directly (to the raw buffer, not the text i/o wrapper:
>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
>>> sys.stdout.buffer
<_io.BufferedWriter name='<stdout>'>
>>> stdout_raw = sys.stdout.buffer
>>> data = b"A"*20 + b"\xef\xbe\xad\xde" + b"A"*4 # notice I'm using bytes literals
>>> stdout_raw.write(data)
AAAAAAAAAAAAAAAAAAAAᆳ�AAAA28