np.array 在特定 excel 单元格中使用 xlsxwrite
np.array with xlsxwrite in specific excel cell
我有一个 np.array,我想将其保存在特定的 excel 单元格中(例如在 B14 中)。
B14 中的输入应如下所示:[[ 0, 540, 1920, 540]]。但是我得到了这个错误:
TypeError: only size-1 arrays can be converted to Python scalars. Unsupported type <class 'numpy.ndarray'> in write()
import xlsxwriter
self.outputs['redlines']=np.array([[0, HEIGHT/2, WIDTH, HEIGHT/2]])
outWorkbook = xlsxwriter.Workbook('parameter.xlsx')
outSheet=outWorkbook.add_worksheet('Tabelle1')
outSheet.write('A13','dts:')
outSheet.write('A14','outputs Redlines:')
outSheet.write('B14', self.dts)
outSheet.write('B14', self.outputs['redlines'])
outWorkbook.close()
谢谢大家!
我不确定你是想写列表中的数据还是数组的字符串表示。我将解决第一个选项。
要使用 xlsxwriter 编写列表,您可以使用工作表 write_row()
或 write_column()
方法(取决于您要写入数据的方向)。
但是,您的 np.array 实际上是一个列表列表,因此您需要在循环中处理它(并对每个子列表使用 write_row()
)或者只选择一个你要。像这样:
outSheet.write_row('B14', self.outputs['redlines'][0])
我有一个 np.array,我想将其保存在特定的 excel 单元格中(例如在 B14 中)。 B14 中的输入应如下所示:[[ 0, 540, 1920, 540]]。但是我得到了这个错误:
TypeError: only size-1 arrays can be converted to Python scalars. Unsupported type <class 'numpy.ndarray'> in write()
import xlsxwriter
self.outputs['redlines']=np.array([[0, HEIGHT/2, WIDTH, HEIGHT/2]])
outWorkbook = xlsxwriter.Workbook('parameter.xlsx')
outSheet=outWorkbook.add_worksheet('Tabelle1')
outSheet.write('A13','dts:')
outSheet.write('A14','outputs Redlines:')
outSheet.write('B14', self.dts)
outSheet.write('B14', self.outputs['redlines'])
outWorkbook.close()
谢谢大家!
我不确定你是想写列表中的数据还是数组的字符串表示。我将解决第一个选项。
要使用 xlsxwriter 编写列表,您可以使用工作表 write_row()
或 write_column()
方法(取决于您要写入数据的方向)。
但是,您的 np.array 实际上是一个列表列表,因此您需要在循环中处理它(并对每个子列表使用 write_row()
)或者只选择一个你要。像这样:
outSheet.write_row('B14', self.outputs['redlines'][0])