在编写csv python3时包含头文件

时间:2018-04-24 17:59:24

标签: python-3.x

我习惯以这种特殊的方式写出CSV,但我 我不知道如何包含标题,因为大多数示例都不是这种格式。

ff = open('data.csv','w')

# Included here would be a typical for loop creating the below variables, i, list_name[i], list_date[i]

ff.write( "%7d, %s, %s\n" % (i, list_name[i], list_date[i]))

1 个答案:

答案 0 :(得分:0)

你可能想要使用csv包,这是一个例子:

import csv

myData = [["first_name", "second_name", "Grade"],
      ['Alex', 'Brian', 'A'],
      ['Tom', 'Smith', 'B']]

myFile = open('file.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

print("done")
相关问题