1

I am a python beginner. I am trying to write multiple lists into separate columns in a csv file.

In my csv file, I would like to have

2.9732676520000001 0.0015852047556142669 1854.1560636319559
4.0732676520000002 0.61902245706737125   2540.1258143280334
4.4032676520000003 1.0                   2745.9167395368572

Following is the code that I wrote.

df=[2.9732676520000001, 4.0732676520000002, 4.4032676520000003]
CS=[1854.1560636319559, 2540.1258143280334, 2745.9167395368572]
int_peak=[0.0015852047556142669, 0.61902245706737125, 1.0]

with open('output/'+file,'w') as f:
    for 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 to object.format this error message. I'm having a hard time to catch what is going wrong. Could anyone spot what's going wrong with my code?

7
  • Are you sure your 'output/' + file is a valid filepath? Commented Jul 27, 2017 at 22:54
  • For what it's worth, it runs in python 2.7 Commented Jul 27, 2017 at 22:54
  • 1
    yes the path is valid. The output file is existent. But it is just blank. Commented Jul 27, 2017 at 22:55
  • @boethius Interesting. I get the following message, "non-empty format string passed to object.__format__" Commented Jul 27, 2017 at 22:57
  • huh. Ran this in 3.6.1 with same code (just different filepath) and it output the desired result.. Commented Jul 27, 2017 at 23:01

2 Answers 2

2

You are better off using pandas

import pandas as 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 a list of lists to a dataframe.
# each list is a row thus after constructing the dataframe
# transpose is applied to get to the user's desired output. 
df = pd.DataFrame([df, int_peak, CS])
df = df.transpose() 

# write the data to the specified output path: "output"/+file_name
# without adding the index of the dataframe to the output 
# and without adding a header to the output. 
# => these parameters are added to be fit the desired output. 
df.to_csv("output/"+file_name, index=False, header=None)

The output CSV looks like this:

2.973268  0.001585  1854.156064
4.073268  0.619022  2540.125814
4.403268  1.000000  2745.916740

However to fix your code, you need to use another file name variable other than file. I changed that in your code as 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"

with open('/tmp/'+file_name,'w') as f:
    for 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 it works. The output is as follows:

2.973268,0.001585,1854.156064
4.073268,0.619022,2540.125814
4.403268,1.000000,2745.916740
Sign up to request clarification or add additional context in comments.

2 Comments

I think the dataframe would need to be rotated to achieve what he is hoping for
@boethius thanks for the comment. I added a transpose to rotate the data.
1

If you need to write only a few selected columns to CSV then you should use following option.

csv_data = df.to_csv(columns=['Name', 'ID'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.