For values ββin the range 0-127, the following line will always return the correct type in Python 2 ( str) and 3 ( bytes):
chr(self.StartElementNew).encode('ascii')
128-255, Python 2 str.encode() str.decode() ASCII , .
0-255 :
if sys.version_info.major >= 3:
as_byte = lambda value: bytes([value])
else:
as_byte = chr
:
self.output.write(as_byte(self.StartElementNew))
six library, six.int2byte() ; Python :
self.output.write(six.int2byte(self.StartElementNew))