0

If I run the commands below, the output differs between Python 2.7 and Python 3.

After spending too much time on this, I think Python 3 is doing some Unicode "magic" and is somehow converting my input (e.g. \xe0 becomes \xc3\xa0).

How can I avoid this support?

How do I get the output from Python 2.7 in Python 3?

python2.7 -c 'print("\x10\x13\xe0\xf7\x60\x42\xdf\xf7\xac\x3b\xf2\xf7")'
python3 -c 'print("\x10\x13\xe0\xf7\x60\x42\xdf\xf7\xac\x3b\xf2\xf7")'

Further example:

If for example I pipe the output of Python into xxd I just want to get what I supplied to print and not any bonus stuff. I am not an experienced programmer but the handling of Python 2.7 seems very intuitive to me. So what must I do to obtain the same behaviour in Python 3?

$ python2.7 -c 'print("\x10\x13\xe0\xf7\x60\x42\xdf\xf7\xac\x3b\xf2\xf7")' | xxd
00000000: 1013 e0f7 6042 dff7 ac3b f2f7 0a         ....`B...;...

$ python3 -c 'print("\x10\x13\xe0\xf7\x60\x42\xdf\xf7\xac\x3b\xf2\xf7")' | xxd
00000000: 1013 c3a0 c3b7 6042 c39f c3b7 c2ac 3bc3  ......`B......;.
00000010: b2c3 b70a
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Green
  • 155
  • 1
  • 2
  • 7
  • 1
    Yes, this is probably the biggest difference between Python 2 and 3 - Python 2 uses byte strings while Python 3 uses Unicode strings. – Mark Ransom Jan 31 '22 at 18:09
  • Are those strings supposed to be text or bytes? And if the latter, what is the encoding? Do you expect the output to show the input decoded as text, or as encoded bytes - or what exactly? – ekhumoro Jan 31 '22 at 18:58
  • 1
    `python3 -c 'import sys; sys.stdout.buffer.write(b"\x10...\xf7")'`. (See the note below the python docs for [sys.stdout](https://docs.python.org/3/library/sys.html#sys.stdout)). – ekhumoro Feb 01 '22 at 17:42
  • I'm not sure I agree with the marked duplicates. Before they will make sense, you have to have a byte string. You can do that by putting a `b` in front of the string like `b"\x10\x13..."`. Or you can do it by encoding the Unicode string like `"\x10\x13...".encode("latin-1")`. – Mark Ransom Feb 06 '22 at 01:16

0 Answers0