How to save many arrays in the same txt in Python3?

Asked

Viewed 378 times

-1

Hello! I have a code in Python3 and in each looping it generates 5 column arrays. For the first looping I was able to record each array as a column in a txt using numpy.savetxt:

array_aux1[int(j/tau), 0] = scale
array_aux2[int(j/tau), 0] = j
array_aux3[int(j/tau), 0] = spectrum_gapped[i]
array_aux4[int(j/tau), 0] = np.real(coef_gapped)
array_aux5[int(j/tau), 0] = np.imag(coef_gapped)

if i == 1:       
    np.savetxt("teste.txt", 
               np.hstack([array_aux1, array_aux2, array_aux3, array_aux4, array_aux5]), 
               fmt = ["%.5e", "%.5e", "%.5e", "%.5e", "%.5e"],
               delimiter = '\t' 
     )       

The problem is that I want to open this file again for all the looping "i’s" and first add a blank line and then save the new generated arrays. I tried to continue the condition by opening the "test.txt" file with the "append" parameter and using np.savetxt again:

else:
    file = open("teste.txt", "a")
    file.write('\n')
    np.savetxt("teste.txt", 
               np.hstack([array_aux1, array_aux2, array_aux3, array_aux4, array_aux5]), 
               fmt = ["%.5e", "%.5e", "%.5e", "%.5e", "%.5e"], 
               delimiter = '\t' 
    )                  

What happens is that the previous matrices are not preserved. It is not the case that the blank line is inserted or the previous results are preserved. What should be done in this case? Does anyone have any suggestions? Thank you.

  • To people putting "downvote" in this question, I would like to know the reason why.

  • I also don’t understand why my question is getting these "downvotes". But this is unusual around here. Soon someone answers and helps!@

  • to the account, it has not been unusual - some users apparently have the mindset of "our I don’t know how to answer this - so I’m going to downvote". The idea of my question above is that people realize what they are doing.

1 answer

1


Note that although you open file with "a" in the second access, you are not passing the file you opened (which is in the variable file) for the function savetxt numpy - instead, you pass only the file name.

And the behavior of numpy.savetxt in this case is to create a new file from scratch, ignoring any previous content. (On Unix systems like Linux and Macos X, the file in the variable file will continue accessible from your program, and you can continue reading and writing on it in your program, but it will no longer be accessible in the file system).

Good, ending: the function savetxt accepts in the first parameter open file type objects, and not only file name strings. So in your case, just make the call by passing file instead of "test.txt" you will get the desired result.

       np.savetxt(file, np.hstack([
           array_aux1, array_aux2, array_aux3,array_aux4, array_aux5]),
           fmt = ["%.5e", "%.5e", "%.5e", "%.5e", "%.5e"], delimiter = '\t' 
       )                  


note: unless you will use this txt as a type of "report" to be viewed directly by people, the txt format is not the best way to preserve this data, for sending to another application, for example. If the data will be processed later in a Python program, it is best to use the module pickle to save this data: it is not human readable, but all attributes of the objects are preserved and recovered transparently.

  • thank you very much. It worked perfectly. I need it to come out exactly in this format because then this txt (which usually gets giant) is used as Input for Gnuplot to generate a graph via command line. I don’t even dare open it. But I’ll read about this pickle module to see if I can be with you soon with matplotlib and generate a similar graph in Python using this data. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.