我试图将信息列表写入python中的.csv文件。到目前为止,我只能打印标题。我想从现有的.csv文件中获取2列,用我的数据写入新的.csv文件。到目前为止,我已经
了def get_id_size():
read_file = pandas.read_csv('poem_info.csv')
poemid = (read_file[['poemname']])
poemsize = (read_file[['poemsize']])
return (poemid, poemsize) #this is the columns i want in my new.csv file
with open('poem_data.csv', 'w') as outfile:
# write header
outfile.write('poemid,poemsize,cli,ari\n')
我的新数据在
cli = calculate_cli
ari = calculate_ari
所以我的问题是:如何将poemid,poemsize,cli和ari分配给csv文件?我是python的新手,我无法弄清楚这样做的语法
答案 0 :(得分:0)
好吧,我不是pandas
大师,但你真的不需要它:
import csv
with open('poem.csv', newline='') as inf,
open('poem_data.csv', 'w', newline='') as outf:
csvout = csv.writer(outf)
csvout.writerow(['poemid', 'poemsize', 'cli', 'ari'])
for (poemid, size, *_), c, a in zip(csv.reader(inf), cli, ari):
csvout.writerow([poemid, size, c, a])
从输入文件中读取(忽略前两个字段之后的所有字段;如果字段实际上是索引1和3,则可以使用for (_, poemid, _, size, *_), c, a in zip(...):
),将它们与计算值配对,然后写入它们出。我确信在pandas
中有更好的方法,但这根本不需要第三方模块,因此在某些情况下可能更为可取。
要将其拆分为函数,您需要在函数外部打开文件或者对结果进行分类(因此with
块在您和&之前不会切断对文件的访问权限#39;重新完成。例如:
def get_id_size():
with open('poem.csv', newline='') as inf:
return [(poemid, size) for poemid, size, *_ in csv.reader(inf)]
cli = calculate_cli()
ari = calculate_ari()
with open('poem_data.csv', 'w', newline='') as outfile:
csvout = csv.writer(outfile)
# write header
csvout.writerow(['poemid', 'poemsize', 'cli', 'ari'])
for (poemid, size), c, a in zip(get_id_size(), cli, ari):
csvout.writerow([poemid, size, c, a])
注意:这是Py3代码; Py2 csv
模块需要进行更改。
答案 1 :(得分:0)
Pandas不是必需的,但它会简化您的生活。您可以执行以下操作(完全删除函数get_id_size()
):
import pandas as pd
df = pd.read_csv('poem_info.csv')
# Change the column name of poemname
df.rename(columns={'poemname':'poemid'}, inplace=True)
# Add the new columns
df['cli'] = calculate_cli()
df['ari'] = calculate_ari()
# Export
df.to_csv('poem_data.csv',header=True,index=False)