Python:写csv标题“打开”

时间:2013-08-13 02:37:19

标签: python csv header

我有以下代码,我在不使用csv模块的情况下生成csv文件,我想知道为此文件执行标头的最佳方法是什么:

for language in languages:
    with open ('user_edits.csv', 'a') as f: 
        f.write(str(user_count)+","+str(language[0])+","+str(language[1])+","+str(language[2])+"\n")

1 个答案:

答案 0 :(得分:1)

只打开一次文件并使用csv模块的功能:

import csv
with open ('user_edits.csv', 'a') as f:
    writer = csv.writer(f, delimiter=',')
    for language in languages:
       writer.writerow([user_count]+language[:3])