easy save/load of data in python

前端 未结 7 1397
小蘑菇
小蘑菇 2020-12-28 09:35

What is the easiest way to save and load data in python, preferably in a human-readable output format?

The data I am saving/loading consists of two vectors of floats

7条回答
  •  滥情空心
    2020-12-28 09:57

    There are several options -- I don't exactly know what you like. If the two vectors have the same length, you could use numpy.savetxt() to save your vectors, say x and y, as columns:

     # saving:
     f = open("data", "w")
     f.write("# x y\n")        # column names
     numpy.savetxt(f, numpy.array([x, y]).T)
     # loading:
     x, y = numpy.loadtxt("data", unpack=True)
    

    If you are dealing with larger vectors of floats, you should probably use NumPy anyway.

提交回复
热议问题