1 回答
TA贡献1868条经验 获得超4个赞
下面是将列追加到现有 CSV 的基本示例:
数据.csv(原始)
col1,col2,col3
1,2,3
4,5,6
7,8,9
test.py
import csv
with open('data.csv',newline='') as fin:
r = csv.DictReader(fin)
lines = list(r)
with open('data.csv','w',newline='') as fout:
# Create writer with existing column headers plus new column headers
w = csv.DictWriter(fout,fieldnames=r.fieldnames + 'angry disgust fear happy sad surprise neutral'.split())
w.writeheader()
for line in lines:
emolist = {'angry': 0.15, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.06, 'sad': 0.24, 'surprise': 0.02, 'neutral': 0.49}
line.update(emolist)
w.writerow(line)
数据.csv (最终)
col1,col2,col3,angry,disgust,fear,happy,sad,surprise,neutral
1,2,3,0.15,0.0,0.05,0.06,0.24,0.02,0.49
4,5,6,0.15,0.0,0.05,0.06,0.24,0.02,0.49
7,8,9,0.15,0.0,0.05,0.06,0.24,0.02,0.49
添加回答
举报