Python 将numpy数组转储到excel文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41870968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dumping numpy array into an excel file
提问by blackbug
I am trying to dump numpy array into an excel file using savetxt method, but I am getting this weird error on the console:
我正在尝试使用 savetxt 方法将 numpy 数组转储到一个 excel 文件中,但我在控制台上收到了这个奇怪的错误:
.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e')
Process finished with exit code 1
And this is the details of the numpy array which i want to dump:
这是我要转储的 numpy 数组的详细信息:
[[[ 0.47185326 0.07954808 0.02773202 ..., 0.05631305 0.08299649
0.03780528]
[ 0.62651747 0.06029205 0.01570348 ..., 0.03766847 0.06287122
0.02287979]
[ 0.72675145 0.04626036 0.0107195 ..., 0.02535284 0.04664176
0.01519825]
...,
[ 0.10476404 0.57678992 0.04675674 ..., 0.02255989 0.06741234
0.0170289 ]
[ 0.13148287 0.47490281 0.06038339 ..., 0.03263607 0.07844847
0.02505469]
[ 0.14134221 0.35699606 0.07600202 ..., 0.04766588 0.09139989
0.0386818 ]]]
Type of first_layer_output : <class 'numpy.ndarray'>
Shape of first_layer_output : (1, 921600, 10)
And I am using savetxt this way:
我以这种方式使用 savetxt:
np.savetxt('test.csv', first_layer_output, delimiter=',')
I am not sure what's wrong here, thus any help will be appreciated :)
我不确定这里出了什么问题,因此任何帮助将不胜感激:)
回答by epattaro
you could use pandas, its really easy friendly to use.
你可以使用pandas,它使用起来非常简单友好。
import pandas as pd
## convert your array into a dataframe
df = pd.DataFrame (array)
## save to xlsx file
filepath = 'my_excel_file.xlsx'
df.to_excel(filepath, index=False)
hope it helps!
希望能帮助到你!
回答by vsdaking
Bit late, but might help others facing such an issue.
有点晚了,但可能会帮助其他人面临这样的问题。
This error is because you appear to have set
此错误是因为您似乎已设置
dtype = np.ndarray
dtype = np.ndarray
when creating / defining your Numpy matrix first_layer_output.
创建/定义您的 Numpy 矩阵 first_layer_output 时。
One solution to this problem is modifying your command storing the
此问题的一种解决方案是修改您的命令存储
np.savetxt('test.csv', first_layer_output, delimiter=',', fmt='%s')
This command now outputs the contents of the array as a string, irrespective the actual data types being used. Hope this helps.
此命令现在将数组的内容作为字符串输出,而不管使用的实际数据类型。希望这可以帮助。