Reputation: 41
I have two numpy arrays with dimensions (81, 5) and (3196, 7) that I need to write to a csv file. The actual desired output would look something like this:
81 #This is the len() of the first array
1 2 3 4 5
.
.
81 2 3 4 5
#skip a line
3196 #len() of the second array
1 2 3 4 5 6 7
.
.
3196 2 3 4 5 6 7
Where the numbers in the columns are some data (not actually 123456). I was able to more or less Frankenstein together an array by filling the spaces with np.NaN then replace the NaN values with blank spaces. However I was unable to output the file using the following:
np.savetxt(r'path/sample.Data.srv', data, delimiter = '\t', fmt='%.5f')
Where I get the error:
Mismatch between array dtype ('object') and format specifier ('%.5f %.5f %.5f %.5f %.5f %.5f %.5f')
Probably due to the blank spaces (' ') being a string. fmt='%.5s' works but I need the numbers to be floats. I am still new to python and have always used np.savetxt() since I have never worked with arrays with different shapes before, nor am I familiar with other options to write txt files. Any help would be greatly appreciated. Thank you.
Upvotes: 0
Views: 2080
Reputation: 231375
Sounds like you cobbled together an array that looks like (with space in place of None):
In [74]: data
Out[74]:
array([[1.0, 1.0, 1.0, 1.0, 1.0, None, None],
[1.0, 1.0, 1.0, 1.0, 1.0, None, None],
[1.0, 1.0, 1.0, 1.0, 1.0, None, None],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], dtype=object)
But the error has more to do with object dtype
In [75]: np.savetxt('test.txt', data, fmt='%.5f')
TypeError: Mismatch between array dtype ('object') and format specifier ('%.5f %.5f %.5f %.5f %.5f %.5f %.5f')
With the more general %s
format, this works:
In [76]: np.savetxt('test.txt', data, fmt='%s')
In [77]: cat test.txt
1.0 1.0 1.0 1.0 1.0 None None
1.0 1.0 1.0 1.0 1.0 None None
1.0 1.0 1.0 1.0 1.0 None None
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
But this is multiple savetxt that I had in mind:
In [79]: with open('test.txt', 'wb') as f:
...: np.savetxt(f, d[0], fmt='%.5f')
...: f.write(b'\n')
...: np.savetxt(f, d[1], fmt='%.5f')
...:
In [80]: cat test.txt
1.00000 1.00000 1.00000 1.00000 1.00000
1.00000 1.00000 1.00000 1.00000 1.00000
1.00000 1.00000 1.00000 1.00000 1.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
Upvotes: 2