Home > Mobile >  How to save computational results using ".write" into a one column .txt?
How to save computational results using ".write" into a one column .txt?

Time:06-23

I performed a computation in Python that results into a numpy array consisting of numerical values. Here is a shortened part of the result:

[-3.92666863e-01 -2.68601261e-01 -2.65401836e-01 -2.34765968e-01
 -2.40034211e-01 -2.38497694e-01 -2.29052660e-01 -2.34477566e-01
 -2.15617829e-01 -2.02265363e-01 -2.14098632e-01 -1.93120651e-01
 -2.09928485e-01 -1.82968909e-01 -1.66702484e-01 -1.47223081e-01
 -1.43542991e-01 -1.31421280e-01 -1.03357537e-01 -8.99456786e-02
 -9.66305953e-02 -9.22995465e-02 -7.08553941e-02 -7.72590531e-02
 -8.38534896e-02 -5.62331513e-02 -6.87916001e-02 -8.25322862e-02
 -9.79528867e-02 -1.28317903e-01 -1.58801111e-01 -1.65446428e-01
 -1.45221084e-01 -1.05980148e-01 -1.13508216e-01 -1.06785745e-01
 -6.16426372e-02 -8.45649741e-02 -7.95497886e-02 -8.23653704e-02]

Now I would like to automatically save the computation results into a .txt file that uses only one column, so that each row contains exactly one value. Here is my current code related to saving the computation results:

Savefile = open("My_file.txt", "w ")
Savefile.write(str(Data))

There are two problems. First, .write cannot save numpy arrays, hence I added str to Savefile.write(str(Data)) to convert the array into a string, since .write only saves strings. Second, as shown above, the result consists of 4 columns.

Is there any way to save the computation results into a one column .txt file?

CodePudding user response:

Savefile.write("\n".join(map(str, Data.tolist())))
  • Related