Python 将numpy数组转储到excel文件中
我试图使用savetxt方法将numpy数组转储到excel文件中,但在控制台上出现了一个奇怪的错误:Python 将numpy数组转储到excel文件中,python,arrays,excel,numpy,Python,Arrays,Excel,Numpy,我试图使用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,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.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
下面是我要转储的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)
我不确定这里出了什么问题,因此任何帮助都将不胜感激:)你可以使用熊猫,它非常容易使用
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)
希望有帮助 有点晚了,但可能会帮助其他人面对这样的问题 此错误是因为您似乎已设置 dtype=np.ndarray 创建/定义Numpy矩阵时,请首先\u层\u输出 此问题的一个解决方案是修改存储
np.savetxt('test.csv', first_layer_output, delimiter=',', fmt='%s')
该命令现在以字符串形式输出数组的内容,而与实际使用的数据类型无关。
希望这能有所帮助。请注意,这依赖于“openpyxl”@zwep is
openpyxl
仅适用于xlsx
文件或所有excel格式文件?@tien从他们的文档中,我可以看到以下“openpyxl是一个Python库,用于读取/写入excel 2010 xlsx/xlsm/xltx/xltm文件。”以及如何将数据写入现有的Excel工作表?谢谢
np.savetxt('test.csv', first_layer_output, delimiter=',', fmt='%s')