简体   繁体   English

Python:将具有不同尺寸的numpy数组写入txt文件

[英]Python: Writing numpy arrays with different dimensions to txt file

I have two numpy arrays with dimensions (81, 5) and (3196, 7) that I need to write to a csv file. 我有两个需要写入csv文件的,尺寸分别为(81,5)和(3196,7)的numpy数组。 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). 列中的数字是一些数据(实际上不是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. 我可以通过用np.NaN填充空格来将科学怪人或多或少地组合在一起,然后用空格替换NaN值。 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') 数组dtype('object')和格式说明符('%.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. fmt ='%。5s'有效,但我需要数字为浮点数。 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. 我对python还是很陌生,并且一直使用np.savetxt(),因为我以前从未使用过具有不同形状的数组,也不熟悉其他用于编写txt文件的选项。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Sounds like you cobbled together an array that looks like (with space in place of None): 听起来就像您将一个看起来像是的数组拼凑在一起(用空格代替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 但是错误与对象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: 使用更通用的%s格式,可以使用:

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: 但这是我想到的多个savetxt:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM