I'm trying to write a Python program that works in both Python 2.7 and Python 3.*. I have a case where I use StringIO, and according to Python-Future's cheatsheet on StringIO, all I have to to is to use the Python 3-style io module.
The problem is that I'm printing floats to this StringIO:
from __future__ import print_function
from io import StringIO
with StringIO() as file:
print(1.0, file=file)
This results in
TypeError: string argument expected, got 'str'
When I replace 1.0 by u"AAAA" (or "AAAA" with unicode_literals enabled), it works fine.
Alternatives I've tried:
BytesIO. I can'tprintanymore, because "unicodedoesn't support the buffer interface"."{:f}".format(...)everyfloat. This is possible, but cumbersome.file.write(...)instead ofprint(..., file=file). This works, but at this point, I don't see what the use ofprint()is anymore.
Are there any other options?