我希望获得如下输出:Output
我能编写这段代码并且工作正常。作为初学者,我在这个代码上寻求批评。我觉得它太冗长了。
当我将此行Exception: Unexpected data type <class 'numpy.int32'>
更改为values=[a*1., a*1.]
时,我不知道为什么会收到此错误values=[a, a]
。为什么它不能导出整数?类似的逻辑在这里工作:
for row, (name, value) in enumerate(zip(names, values)):
sh.write(row, col, name);
sh.write(row, col+1, value);
import numpy as np
import xlwt #THIS MAY BE GOOD
#from pandas import DataFrame
amin =2
amax=10
da = 2
names = ['amin', 'amax', 'da']
values = [amin, amax, da]
fname='EXPORTexample.xls'; sheetname = 'sheet 1'
book = xlwt.Workbook()
sh = book.add_sheet(sheetname)
col=0;
# FIRST WRITE THE VARIABLES AND THEIR VALUES INTO XLSX.
for row, (name, value) in enumerate(zip(names, values)):
sh.write(row, col, name);
sh.write(row, col+1, value);
t2 = np.arange(0.0, 5.0, 1)
amat= np.arange(amin,amax,da)
fn=np.zeros((np.shape(t2)[0],1))
col=3;
#row_eps =0;
#row_Dt += row_eps;
for a in amat:
fn = a*t2
names = ['eps_dot', 'Delta_t']
values=[a*1., a*1.]
#write specific a conditions on every data curve/set
for row, (name, value) in enumerate(zip(names, values)):
sh.write(row, col, name);
sh.write(row, col+1, value);
#write names such as strain, stress, d11s, d_dot, d11
occ_rows = len(names)#no.of occupied rows.
names = ['strain', 'stress', 'd11s', 'd_dot', 'd11']
for cols,name in enumerate(names):
sh.write(occ_rows+1, col+cols, name)
## WRITE actual numbers
for rows, (strain_i,stress_i, d11s_i, d_dot_i, d11_i) in enumerate(zip(fn, t2, fn, t2, fn)):
sh.write(occ_rows+1+rows+1, col, strain_i)
sh.write(occ_rows+1+rows+1, col+1, stress_i)
sh.write(occ_rows+1+rows+1, col+2, d11s_i)
sh.write(occ_rows+1+rows+1, col+3, d_dot_i)
sh.write(occ_rows+1+rows+1, col+4, d11_i)
col+=6 #incremening column value to avoid overwriting and related error
book.save(fname)