1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
import csv
with open('text1.csv','a') as csvfile: wr = csv.writer(csvfile) wr.writerow(['id','name','age']) wr.writerow(['1','Tom',12])
with open('t1.csv','w') as csvfile: wr2 = csv.writer(csvfile,delimiter=' ') wr2.writerow(['id','name','age']) wr2.writerow(['1','Tom',12]) wr2.writerow(['1','Tom',12]) wr2.writerow(['1','Tom',12])
with open('ts.csv','a') as file: wr3 = csv.writer(file) wr3.writerow(['id','name','age']) contents = [[1,'Paul',12],[2,'Gairly',18]] wr3.writerows(contents)
with open('dic.csv','a') as file: d1 = {'id':1,'name':'Enum','age':34} d2 = {'id':2,'name':'Spliey','age':27} filename = [ 'id','name','age'] wd = csv.DictWriter(file,fieldnames=filename) wd.writeheader() wd.writerow(d1) wd.writerow(d2)
with open('dzw.csv','a',encoding='utf-8') as file: dz = {'id':1,'name':'看嘛','age':12} filename1 = ['id','name','age'] wd1 = csv.DictWriter(file, fieldnames=filename1) wd1.writeheader() wd1.writerow(dz) wd1.writerow(d1)
with open('dzw.csv','r',encoding='utf-8') as file: reader = csv.reader(file) for tx in reader: print(tx)
|