python - Formatting columns when writing to csv -
i'm trying write following list of lists columns in .csv file, using python.
writelist = [['met ', '===============', 'et_miss_inv', 'et_miss_lep', 'et_miss_seed', 'met_avpt_seed'], ['photon ', '===============', 'pass1phtriggers', 'nph', 'topoetcone40_1ph', 'ptvarcone20_1ph', 'pt_1ph', 'eta_1ph', 'phi_1ph', 'dphi_1ph', 'dphi_1ph1jet', 'phi_v', 'nbaselineph', 'dphimin4_lep'], ['weight ', '===============', 'j1_bweight', 'j1_bweight', 'j1_bweight', 'j2_bweight', 'j2_bweight', 'j2_bweight', 'leptonweight', 'triggerweight'], ['hlt ', '===============', 'hlt_g120_loose', 'hlt_g140_loose', 'hlt_e20_medium', 'hlt_2e17_loose', 'hlt_mu20_iloose_l1mu15', 'hlt_mu26', 'hlt_2mu6', 'hlt_mu50'], ['truth ', '===============', 'pt_1jet_truth'], ['other ', '===============', 'rg', 'nbjets_seed', 'mctcorr2b', 'e_v', 'pt_v', 'pt_1jet_seed', 'topness', 'centrality', 'btagzerolep_channel', 'allghostgood', 'averageintperxingcorr', 'coreflag', 'btag0l_channel', 'sumet_refjet_seed', 'pt_2jet_seed', 'istruthanalysis', 'mctcorrjj', 'pass1ltriggers', 'eta_v', 'pt_b']]
the first 2 elements of every sub-lists decorative (title+separation). i'm trying have columns evenly spaced tabs, so:
import csv itertools import izip_longest writelist = [['met ', '===============', 'et_miss_inv', 'et_miss_lep', 'et_miss_seed', 'met_avpt_seed'], ['photon ', '===============', 'pass1phtriggers', 'nph', 'topoetcone40_1ph', 'ptvarcone20_1ph', 'pt_1ph', 'eta_1ph', 'phi_1ph', 'dphi_1ph', 'dphi_1ph1jet', 'phi_v', 'nbaselineph', 'dphimin4_lep'], ['weight ', '===============', 'j1_bweight', 'j1_bweight', 'j1_bweight', 'j2_bweight', 'j2_bweight', 'j2_bweight', 'leptonweight', 'triggerweight'], ['hlt ', '===============', 'hlt_g120_loose', 'hlt_g140_loose', 'hlt_e20_medium', 'hlt_2e17_loose', 'hlt_mu20_iloose_l1mu15', 'hlt_mu26', 'hlt_2mu6', 'hlt_mu50'], ['truth ', '===============', 'pt_1jet_truth'], ['other ', '===============', 'rg', 'nbjets_seed', 'mctcorr2b', 'e_v', 'pt_v', 'pt_1jet_seed', 'topness', 'centrality', 'btagzerolep_channel', 'allghostgood', 'averageintperxingcorr', 'coreflag', 'btag0l_channel', 'sumet_refjet_seed', 'pt_2jet_seed', 'istruthanalysis', 'mctcorrjj', 'pass1ltriggers', 'eta_v', 'pt_b']] open("result.csv","w+") f: writer = csv.writer(f, delimiter = '\t') values in izip_longest(*writelist): writer.writerow(values)
however, can checked opening result.csv
, alignment isn't coming out right. how can fix it?
look str.format
padding , justification of text.
https://pyformat.info/#string_pad_align https://docs.python.org/2/library/string.html#formatstrings
Comments
Post a Comment