为什么我使用np.savetxt时数组空间不正确 [英] Why doesn't my array space correctly when I use np.savetxt

查看:74
本文介绍了为什么我使用np.savetxt时数组空间不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么文本文件不能正确分配输出空间?

Why doesn't the text file space the output correctly?

import numpy as np
my_list =['str1', 'str2', 'str3']
my_list2=[1,2,3]
print(my_list)

['str1','str2','str3']已打印。

['str1', 'str2', 'str3'] is printed.

my_array=np.array(my_list)
my_array2=np.array(my_list2)
combined=np.column_stack([my_array,my_array2])
np.savetxt('test_file_name.txt', combined, fmt="%s")
print(combined)

打印给出:

[['str1' '1']

['str2' '2']

['str3' '3']]

文件说 str1 1str2 2str3 3
我想说:

The file says str1 1str2 2str3 3. I want it to say:

str1 1

str2 2

str3 3

运行Windows 8.1,使用记事本查看文件。

Running Windows 8.1, using notepad to view file.

简短答案:记事本最差。使用记事本++。

Short Answer: Notepad is the worst. Use notepad++.

附加一个问题:有没有办法使列排列的更好?我的一些数据只有几个字符,而其他的则只有15个左右。

Appending a question: Is there a way to have the columns line up nicer? Some of my data has only a few characters, while others are have fifteen or so.

推荐答案

如果打印数组,您将如果您在打印时使用 repr ,即 print repr(np.array([1,2,3 ]))或从python shell或ipython运行它而不进行打印,它还会显示 repr ,这就是您在scipy文档中看到的内容。

If you print the array you will not see the commas which is the normal behaviour numpy, if you use repr when printing i.e print repr(np.array([1, 2, 3])) or run it from a python shell or ipython without printing it will also show the repr which is what you see in the scipy docs.

无论您看到的是repr输出还是格式化的输出,都没有任何区别,数组的形状不会改变,因此与您的问题无关:

There is no difference at all whether you see the repr output or the formatted output, the array shape does not change so that has nothing to do with your issue:

In [13]: a
Out[13]: 
array(['1', '2', '3'],  # repr
      dtype='|S1')

In [14]: b
Out[14]: 
array(['2', '3', '4'], # repr
      dtype='|S1')

In [15]: print(a) # str
['1' '2' '3']

In [16]: print(b) # str
['2' '3' '4']

In [17]: print(np.column_stack((a,b)))
[['1' '2']
 ['2' '3']
 ['3' '4']]

如果您遇到 column_stack 的问题,这并不是因为打印数组时的外观。

If you are having issues with column_stack, it is not because of how the arrays look when you print them.

您的输出也将与文件中的内容完全一样:

Your output would also be exactly as you want it in your file:

str1 1
str2 2
str3 3

我看到你得到<$ c的唯一方法文件中的$ c> str1 1str2 2str3 3 是如果您实际使用了 newline = ,所以所有数据都将以一行结尾。

The only way I see you getting str1 1str2 2str3 3 in the file is if you actually used newline="" so all the data would end up on a single line.

这篇关于为什么我使用np.savetxt时数组空间不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆