|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:我想把2个字典写入,如图中效果该怎么做。
- import csv
- headers = ['class','name','sex','height','year','','class','name','sex','height','year']
- rows = [
- {'class':1,'name':'xiaoming','sex':'male','height':168,'year':23},
- {'class':1,'name':'xiaohong','sex':'female','height':162,'year':22},
- {'class':2,'name':'xiaozhang','sex':'female','height':163,'year':21},
- {'class':2,'name':'xiaoli','sex':'male','height':158,'year':21},
- ]
- row = [
- {'class':3,'name':'xiaoming','sex':'male','height':168,'year':23},
- {'class':3,'name':'xiaohong','sex':'female','height':162,'year':22},
- {'class':4,'name':'xiaozhang','sex':'female','height':163,'year':21},
- {'class':4,'name':'xiaoli','sex':'male','height':158,'year':21},
- ]
- list1=[]
- list1.append(rows)
- list1.append(row)
- with open('ttt2.csv','w',newline='')as f:
- f_csv = csv.DictWriter(f,headers)
- f_csv.writeheader()
- f_csv.writerows(row)
复制代码 |
|