bytestypes in Python 3
Written on 10 Feb 2014
Python 3 handles strings a bit different. Originally there was just one type for
str. When unicode gained traction in the ’90s the new
was added to handle Unicode without breaking pre-existing code1. This is
effectively the same as
str but with multibyte support.
In Python 3 there are two different types:
bytestype. This is just a sequence of bytes, Python doesn’t know anything about how to interpret this as characters.
strtype. This is also a sequence of bytes, but Python knows how to interpret those bytes as characters.
unicodetype was dropped.
strnow supports unicode.
In Python 2 implicitly assuming an encoding could cause a lot of problems; you
could end up using the wrong encoding, or the data may not have an encoding at
all (e.g. it’s a PNG image).
Explicitly telling Python which encoding to use (or explicitly telling it to guess) is often a lot better and much more in line with the “Python philosophy” of “explicit is better than implicit”.
This change is incompatible with Python 2 as many return values have changed,
leading to subtle problems like this one; it’s probably the main reason why
Python 3 adoption has been so slow. Since Python doesn’t have static typing2
it’s impossible to change this automatically with a script (such as the bundled
bytes('h€llo', 'utf-8'); this should produce
b'H\xe2\x82\xacllo'. Note how one character was converted to three bytes.
Of course, UTF-8 may not be the correct character set in your case, so be sure to use the correct one.
In your specific piece of code,
nextline is of type
subprocess changed in Python 3 from
bytes. This is because Python can’t be sure which encoding this uses. It
probably uses the same as
sys.stdin.encoding (the encoding of your system),
but it can’t be sure.
You need to replace:
You will also need to modify
if nextline == '' to
if nextline == b'' since:
>>> '' == b'' False
There are some neat tricks you can do with ASCII that you can’t do with multibyte character sets; the most famous example is the “xor with space to switch case” (e.g.
chr(ord('a') ^ ord(' ')) == 'A') and “set 6th bit to make a control character” (e.g.
ord('\t') + ord('@') == ord('I')). ASCII was designed in a time when manipulating individual bits was an operation with a non-negligible performance impact. ↩
Yes, you can use function annotations, but it’s a comparatively new feature and little used. ↩