5

I need to write 3 numpy arrays into a txt file. The head of the file looks like that:

#Filexy
#time  operation1 operation2

The numpy arrays look like the following:

time = np.array([0,60,120,180,...])
operation1 = np.array([12,23,68,26,...)]
operation2 = np.array([100,123,203,301,...)]

In the end, the .txt file should look like this (delimiter should be a tab):

#Filexy
#time  operation1 operation2
0   12   100
60  23    123
120  68   203
180  26   301
..  ...   ...

I tried it with "numpy.savetxt" - but I did not get the format that I want.

Thank you very much for your help!

Ébe Isaac
  • 8,387
  • 11
  • 48
  • 79
Matias
  • 521
  • 1
  • 4
  • 12

2 Answers2

4

I'm not sure what you tried, but you need to use the header parameter in np.savetxt. Also, you need to concatenate your arrays properly. The easiest way to do this is to use np.c_, which forces your 1D-arrays into 2D-arrays, and then concatenates them the way you expect.

>>> time = np.array([0,60,120,180])
>>> operation1 = np.array([12,23,68,26])
>>> operation2 = np.array([100,123,203,301])
>>> np.savetxt('example.txt', np.c_[time, operation1, operation2],
               header='Filexy\ntime  operation1 operation2', fmt='%d',
               delimiter='\t')

example.txt now contains:

# Filexy
# time  operation1 operation2
0   12  100
60  23  123
120 68  203
180 26  301

Also note the usage of fmt='%d' to get integer values in the output. savetxt will save as float by default, even for an integer array.

Regarding the delimiter, you just need to use the delimiter argument. It's not clear here, but there are, in fact, tabs between the columns. For instance, vim shows me tabs using dots:

# Filexy
# time  operation1 operation2
0·  12· 100
60· 23· 123
120·68· 203
180·26· 301

Addendum:

If you want to add headers and add an extra line before the arrays, you are better off creating a custom header, complete with your own comment characters. Use the comment argument to prevent savetxt from adding extra #'s.

>>> extra_text = 'Answer to life, the universe and everything = 42'
>>> header = '# Filexy\n# time operation1 operation2\n' + extra_text
>>> np.savetxt('example.txt', np.c_[time, operation1, operation2],     
               header=header, fmt='%d', delimiter='\t', comments='')

which produces

# Filexy
# time operation1 operation2
Answer to life, the universe and everything = 42
0   12  100
60  23  123
120 68  203
180 26  301
Praveen
  • 5,582
  • 3
  • 39
  • 56
  • 1
    Don't forget `delimiter='\t'` – juanpa.arrivillaga Sep 14 '16 at 06:38
  • Great thank you! One additional thing --> how could i write in the third line (before the arrays start) a line that does not start with # ( in my case it would be just a number) – Matias Sep 14 '16 at 06:49
  • Thank you again - I tried your Addendum - but i missed the extra_num :). If possible the code should look like this: first line "#Filexy; second line "#time operation1 operation2; third line "42 (without a #); fourth --end..: the arrays. Thank you so much for helping me! – Matias Sep 14 '16 at 06:58
  • Please also see http://stackoverflow.com/questions/39487605/how-to-write-numpy-arrays-to-txt-file-starting-at-a-certain-line-numpy-versio :) thank you – Matias Sep 14 '16 at 09:58
0

Try this:

f = open(name_of_the_file, "a")
np.savetxt(f, data, newline='\n')
f.close()
lskrinjar
  • 4,581
  • 6
  • 26
  • 52