我想有列标题:t1
,sigma
,P_calc
,Vel
只在最顶部,而不是在每一步之前。附加当前输出
import csv
import InvasionC
with open('Data.csv', 'w') as f:
for x in range(0,len(InvasionC.t1)):
#print(Pe.Pe[x])
print(InvasionC.Isigma) #for pressure
print(InvasionC.IX) #for velocity
print(InvasionC.IVelprof)
writer = csv.writer(f)
writer.writerow(['t1', 'sigma', 'P_calc', 'Vel'])
writer.writerows(zip([InvasionC.t1[x]],[InvasionC.Isigma[x]],[InvasionC.IX[x]],[InvasionC.IVelprof[x]]))
当前输出为
输入图片描述
如果第一行是标题,请尝试使用索引获取约束,如下所示
import csv
import InvasionC
with open('Data.csv', 'w') as f:
for x in range(0,len(InvasionC.t1)):
writer = csv.writer(f)
line = zip([InvasionC.t1[x]],[InvasionC.Isigma[x]],[InvasionC.IX[x]],[InvasionC.IVelprof[x]]) if x else ['t1', 'sigma', 'P_calc', 'Vel']
writer.writerow(line)