Home > Back-end >  Column header format in Python
Column header format in Python

Time:06-22

I want to have the column headers: t1, sigma, P_calc, Vel only at the very top, not before every step. The current output is attached.

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]]))

The current output is

enter image description here

CodePudding user response:

If your first line is a heading, try using an indexed fetch for constraints, like this

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)
  • Related