Indicating format of multiple numbers in numpy.savetxt

被刻印的时光 ゝ 提交于 2021-01-29 18:03:01

问题


I want to save an array using numpy.savetxt. The array contains eight numbers. Only the format of the first number is different with respect to the latter seven. I know I can set the format of numbers as follows:

numpy.savetxt(filename, array, fmt = "%03d" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f")

Here filename is just the name of my file e.g. numbers.dat and array is a 1D numpy array containing my eight numbers.

The above line of code works but looks ridiculous because I'm specifying each individual format of my numbers. How can I indicate that the latter seven numbers have the same format in a pythonic way?

Thx!


回答1:


You could just simplify it:

"%03d" + "%.10f"*7


来源:https://stackoverflow.com/questions/23117490/indicating-format-of-multiple-numbers-in-numpy-savetxt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!