0

I have two arrays (size 6x3) and would like to write the content into a file.

pos_pb_now = np.array(pos_pb_now, dtype='f')
pos_pw_now = np.array(pos_pw_now, dtype='f')

and

np.savetxt(f_store_handler, pos_pb_now, fmt='%5.2f')

gives

  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1215, in savetxt
    fh.write(asbytes(format % tuple(row) + newline))
TypeError: write() argument must be str, not bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/family/glade/game_uwr/game_uwr.py", line 871, in store_coord
    np.savetxt(f_store_handler, pos_pb_now, fmt='%5.2f')
  File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1219, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('float32') and format specifier ('%5.2f %5.2f %5.2f')

Has anybody an idea how to solve the issue? So far no success in looking at several explanation in python forums.

Background: the 6 are 6 players of a team, moving in 3 dimensions (space). The 2 arrays represent 2 teams (white and blue). By storing their movements into a file (with append), I can call their 3D movement later (simulate/calculate in excel) and make match analysis. A bit like a soccer analysis but in 3D. It will be a tool for the trainer to explain to beginners how to go at the right position and make the right movement.

shape = (6, 3) example

[[ 0.83333331  1.          4.        ]
 [ 2.5         1.          4.        ]
 [ 4.16666651  1.          4.        ]
 [ 5.83333349  1.          4.        ]
 [ 7.5         1.          4.        ]
 [ 9.16666698  1.          4.        ]]

dtype = float32

floppy_molly
  • 175
  • 1
  • 10
  • It should work. What is `pos_pb_now.shape` and `pos_pb_now.dtype'? A few sample rows might also help. – hpaulj Feb 13 '18 at 17:33
  • I added the output of print(pos_pb_now.shape) print(pos_pb_now) print(pos_pb_now.dtype) into previous post – floppy_molly Feb 13 '18 at 18:14
  • from here https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file import pandas as pd df = pd.DataFrame(pos_pb_now) df.to_csv(filename_coord_store, header=None, index=None) this work. But I would like to avoid pandas. Any idea is welcome. – floppy_molly Feb 15 '18 at 19:54

1 Answers1

0

If your array is as you describe it, a (6,3) float, your savetxt should work

In [162]: arr = np.arange(18.).reshape(6,3)
In [163]: arr
Out[163]: 
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.],
       [ 9., 10., 11.],
       [12., 13., 14.],
       [15., 16., 17.]])
In [164]: np.savetxt('test', arr, fmt='%5.2f')
In [165]: cat test
 0.00  1.00  2.00
 3.00  4.00  5.00
 6.00  7.00  8.00
 9.00 10.00 11.00
12.00 13.00 14.00
15.00 16.00 17.00
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • according a post at stackoverflow, the solution is, open the file with an additionnal "b" to the "w" and, now, it works. f_store_handler = open(filename_coord_store, 'wb') Topic closed. Thanks all for the help and support. – floppy_molly Feb 16 '18 at 20:07
  • @floppy_molly, I didn't pay attention to the variable name `f_store_handler`. If it is an already open file, then the write mode can make difference. `savetxt` used to require a 'bw' file. Though it looks like the latest release, 1.14 appears to handle the 'w' mode just fine. Originally `savetxt` was trying to keep the Py2 bytestring mode in Py3. – hpaulj Feb 16 '18 at 20:14