I would like to convert a numpy array into a string representation with a given format. This
from io import BytesIO
import numpy
data = numpy.random.rand(5)
s = BytesIO()
numpy.savetxt(s, data, "%.15e")
out = s.getvalue().decode()
print(out)
3.208726298090422e-01
6.817590490300521e-01
3.446035342640975e-01
7.871066165361260e-01
4.829308426574872e-01
works, but savetxt is slow. tofile is about twice as fast, but I don't know how to get it to work with BytesIO. Perhaps there is another alternative.
Any hints?
savetxtcreates afmtstring by replicating that%...to match the number of columns indata. It then doesfmt % tuple(row). The Python string formatting mechanism works on individual numbers (and by extension tuples of numbers), not on arrays.