python 写csv文件

 

 1.直接写

import csv
'''
headers=['Summary', 'IssueType', 'Status', 'Priority', 'Assignee', 'Components']
rows1=['summary1','task1','do do','hi','ui1','com1']
rows2=['summary2','task2','do do2','hi2','ui2','com2']
rows3=['summary2','task2','do do2','hi2','ui2','com2']
with open(r'C:\Temp\ff.csv','a',newline='') as f:
    f_csv=csv.writer(f,dialect='excel')
    f_csv.writerow(headers)
    f_csv.writerow(rows1)
    f_csv.writerow(rows2)
    f_csv.writerow(rows3)
'''

rows4=['summary4','task4','do do4','hi','ui4','com4']
rows5=['summary5','task5','do do5','hi2','ui5','com5']
rows6=['summary6','task6','do do6','hi2','ui6','com6']
with open(r'C:\Temp\ff.csv','a',newline='') as f:
    f_csv=csv.writer(f,dialect='excel')
    f_csv.writerow(rows4)
    f_csv.writerow(rows5)
    f_csv.writerow(rows6)

 

 

2.字典数据系列写入

import csv
headers=['Summary', 'IssueType', 'Status', 'Priority', 'Assignee', 'Components']
rows7={'Summary':'summary7','IssueType':'task7','Status':'do do7','Priority':'hi7','Assignee':'ui7','Components':'com7'}
rows8={'Summary':'summary8','IssueType':'task8','Status':'do do8','Priority':'hi8','Assignee':'ui8','Components':'com8'}
with open(r'C:\Temp\ff.csv','a',newline='') as f:
    f_csv=csv.DictWriter(f,headers)
    f_csv.writeheader()
    f_csv.writerow(rows7)
    f_csv.writerow(rows8)

 

如果是追加就不需要header, 去掉 f_csv.writeheader()

 

posted @ 2019-02-21 16:16  筱筱的春天  阅读(359)  评论(0编辑  收藏  举报