python怎样创建,读写,追加csv文件
创建:
利用csv包中的writer函数,如果文件不存在,会自动创建,需要注意的是,文件后缀一定要是.csv,这样才会创建csv文件
这里创建好文件,将csv文件的头信息写进了文件。
import csv def create_csv(): path = "aa.csv" with open(path,'wb') as f: csv_write = csv.writer(f) csv_head = ["good","bad"] csv_write.writerow(csv_head)
追加:
在python中,以a+的方式打开,是追加
def write_csv(): path = "aa.csv" with open(path,'a+') as f: csv_write = csv.writer(f) data_row = ["1","2"] csv_write.writerow(data_row)
读:
利用csv.reader可以读csv文件,然后返回一个可迭代的对象csv_read,我们可以直接从csv_read中取数据
def read_csv(): path = "aa.csv" with open(path,"rb") as f: csv_read = csv.reader(f) for line in csv_read: print line