这些Python打印命令在Python 2和Python 3中表现不同的原因是什么。

huangapple go评论99阅读模式
英文:

Why these python print commands behave differently with Python 2 and Python 3

问题

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

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

得分: 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:

&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.stdout
&lt;_io.TextIOWrapper name=&#39;&lt;stdout&gt;&#39; mode=&#39;w&#39; encoding=&#39;utf-8&#39;&gt;
&gt;&gt;&gt; sys.stdout.buffer
&lt;_io.BufferedWriter name=&#39;&lt;stdout&gt;&#39;&gt;
&gt;&gt;&gt; stdout_raw = sys.stdout.buffer
&gt;&gt;&gt; data = b&quot;A&quot;*20 + b&quot;\xef\xbe\xad\xde&quot; + b&quot;A&quot;*4 # notice I&#39;m using bytes literals
&gt;&gt;&gt; stdout_raw.write(data)
AAAAAAAAAAAAAAAAAAAAᆳ�AAAA28

huangapple
  • 本文由 发表于 2023年8月11日 01:01:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定