我有以下代码,我在不使用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")
答案 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])