I'm trying to write a formatted string containing Cyrillic symbols (in utf-8) to Unix pipe:
sort_proc.stdin.write("{}\n".format(cyrillic_text).decode('utf-8').encode('utf-8'))
I had to encode because 'str' does not support the buffer interface and decode because 'ascii' codec can't decode byte 0xd0. So this code works in Python 2.7 as expected. However Python 3.4 says 'str' object has no attribute 'decode' as string literals in python3 are already "decoded". So I know how to fix it for each version separately but don't know how to fix for both. I've found a solution related to reloading sys module and setting setdefaultencoding, however this article why should we NOT use sys.setdefaultencoding says it's just a hack and shouldn't be used at all. Please post the most pythonic way of doing these things. Thanks.