我正在尝试使用 xlsxwriter 将 numpy.ndarray 写入 excel 电子表格。
我数组的“类型”是 numpy.ndarray。打印时数组本身采用以下形式:
exampleArray = array([[[0. , 1], [0. , 1], [0. ,1]]])
我使用的代码如下:
workbook = xlsxwriter.Workbook('WorkBookName.xlsx')
worksheet = workbook.add_worksheet('workSheetName')
row = 0
col = 0
for data in exampleArray[0]:
worksheet.write_colum(row, col data)
col += 1
workbook.close()
我得到错误:
AttributeError: 'list' 对象没有属性 'tell'
提前致谢!
最佳答案
使用 Pandas,这很简单。您只需要确保数组是二维的。
import pandas as pd
A = np.array([[[0. , 1], [0. , 1], [0. ,1]]])
# construct dataframe, index [0] to make 2d
df = pd.DataFrame(A[0])
# save to Excel, exclude index and headers
df.to_excel('file.xlsx', index=False, header=False)
关于python - 将 numpy.ndarray 写入 xlsx 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904126/