I want to write a non-ascii character, lets say → to standard output. The tricky part seems to be that some of the data that I want to concatenate to that string is read from json. Consider the follwing simple json document:
{"foo":"bar"}
I include this because if I just want to print → then it seems enough to simply write:
print("→")
and it will do the right thing in python2 and python3.
So I want to print the value of foo together with my non-ascii character →. The only way I found to do this such that it works in both, python2 and python3 is:
getattr(sys.stdout, 'buffer', sys.stdout).write(data["foo"].encode("utf8")+u"→".encode("utf8"))
or
getattr(sys.stdout, 'buffer', sys.stdout).write((data["foo"]+u"→").encode("utf8"))
It is important to not miss the u in front of → because otherwise a UnicodeDecodeError will be thrown by python2.
Using the print function like this:
print((data["foo"]+u"→").encode("utf8"), file=(getattr(sys.stdout, 'buffer', sys.stdout)))
doesnt seem to work because python3 will complain TypeError: 'str' does not support the buffer interface.
Did I find the best way or is there a better option? Can I make the print function work?
print(data['foo'] + u'→')doesn't work?print, in Python 3 encoding the string returnsbytes. Sinceprintrequires a string, it calls the__str__method, which forbytesjust returns a repr, i.e.str("→".encode()) == "b'\\xe2\\x86\\x92'". Nextprintwrites this useless repr to thefile, but theBufferedWriterrequires an object that supports the buffer interface, such asbytes.print()is able to print all kinds of datatypes without explicit conversion tostrI didnt think it would choke onbytes.bytes. Decodingbytesusing a default encoding would be wrong in general, since abytesobject isn't necessarily text. I just meant the repr string is "useless" for your needs. What choked is trying to print to aBufferedWriter, e.g.print('abc', file=sys.stdout.buffer).