ValueError: Unknown format code 'f' for object of type 'str'

13,747

The easiest way with numpy.savetxt:

import numpy as np
a = np.arange(5)
b = np.arange(5) + 2
np.savetxt('test.txt', np.array([a,b]).T, '%f')

Gives:

0.000000 2.000000
1.000000 3.000000
2.000000 4.000000
3.000000 5.000000 
4.000000 6.000000
Share:
13,747
Moksha
Author by

Moksha

Updated on June 04, 2022

Comments

  • Moksha
    Moksha almost 2 years

    I get the following error when trying to write 2 NumPy arrays on a DAT file.

    fo.write('{0:f} {1:f} \n'.format(np.array(p_initial),np.array(pv_za)))
    ValueError: Unknown format code 'f' for object of type 'str'
    

    The full code is available here : TypeError: can't multiply sequence by non-int of type 'float' : prblem with NumPy arrays

    I am quite new to Python. How does one write NumPy floating point arrays to DAT files so that they can be plotted using matplotlib or gnuplot ?

  • Marcin Pietraszek
    Marcin Pietraszek about 11 years
    Please provide me information why my answer after edition isn't useful.
  • Moksha
    Moksha about 11 years
    I didn't vote down your answer Marcin!... That error is not showing. Now I get the error again I was having pv_za.append(K*np.sin(K*pv_za_temp)) TypeError: can't multiply sequence by non-int of type 'float' :((
  • tiago
    tiago about 11 years
    That doesn't work because the OP wants to print the entire arrays in one line, not array elements.