Writing multiple columns into csv files using python -


i python beginner. trying write multiple lists separate columns in csv file.

in csv file, have

2.9732676520000001 0.0015852047556142669 1854.1560636319559 4.0732676520000002 0.61902245706737125   2540.1258143280334 4.4032676520000003 1.0                   2745.9167395368572 

following code wrote.

df=[2.9732676520000001, 4.0732676520000002, 4.4032676520000003] cs=[1854.1560636319559, 2540.1258143280334, 2745.9167395368572] int_peak=[0.0015852047556142669, 0.61902245706737125, 1.0]  open('output/'+file,'w') f:     dt,int_norm,css in zip(df,int_peak,cs):         f.write('{0:f},{1:f},{2:f}\n'.format(dt,int_norm,css))  

this isn't running properly. i'm getting non-empty format string passed object.format error message. i'm having hard time catch going wrong. spot what's going wrong code?

you better off using pandas

import pandas pd  df=[2.9732676520000001, 4.0732676520000002, 4.4032676520000003] cs=[1854.1560636319559, 2540.1258143280334, 2745.9167395368572] int_peak=[0.0015852047556142669, 0.61902245706737125, 1.0]  file_name = "your_file_name.csv"  # pandas can convert list of lists dataframe. # each list row after constructing dataframe # transpose applied user's desired output.  df = pd.dataframe([df, int_peak, cs]) df = df.transpose()   # write data specified output path: "output"/+file_name # without adding index of dataframe output  # , without adding header output.  # => these parameters added fit desired output.  df.to_csv("output/"+file_name, index=false, header=none) 

the output csv looks this:

2.973268  0.001585  1854.156064 4.073268  0.619022  2540.125814 4.403268  1.000000  2745.916740 

as fix code, need use file name variable other file. changed in code follows:

df=[2.9732676520000001, 4.0732676520000002, 4.4032676520000003] cs=[1854.1560636319559, 2540.1258143280334, 2745.9167395368572] int_peak=[0.0015852047556142669, 0.61902245706737125, 1.0]  file_name = "your_file_name.csv"  open('/tmp/'+file_name,'w') f:     dt,int_norm,css in zip(df,int_peak,cs):         f.write('{0:f},{1:f},{2:f}\n'.format(dt,int_norm,css)) 

and works. output follows:

2.973268,0.001585,1854.156064 4.073268,0.619022,2540.125814 4.403268,1.000000,2745.916740 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -