简体   繁体   English

使用python将多列写入csv文件

[英]Writing multiple columns into csv files using python

I am a python beginner.我是一个蟒蛇初学者。 I am trying to write multiple lists into separate columns in a csv file.我正在尝试将多个列表写入 csv 文件中的单独列。

In my csv file, I would like to have在我的 csv 文件中,我想要

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?谁能发现我的代码出了什么问题?

You are better off using pandas你最好使用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:输出 CSV 如下所示:

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 .但是,要修复您的代码,您需要使用 file 以外的另一个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

If you need to write only a few selected columns to CSV then you should use following option.如果您只需要将几个选定的列写入 CSV,那么您应该使用以下选项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM