我想在csv文件每行的末尾附加一个值列表,但标题除外。
我已经尝试过以下代码,但是它会删除csv文件中的所有行,并且不会将任何值附加到csv文件中。
import tensorflow
from fer.fer import FER
import cv2
from os import listdir
from os.path import isfile, join
import numpy
import csv
f1 = open("C:/Yasir/Thesis/4/FaceDetectionLatest/bin/Debug/Data/demoScoringData9.csv", "a+")
reader = csv.reader(f1)
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
# cv2.imshow(join(mypath,onlyfiles[n]),0)
img = cv2.imread( join(mypath,onlyfiles[n]) )
detector = FER()
emotion = detector.detect_emotions(img)
dict = emotion[0]
emoList = dict['emotions']
dict_writer = csv.DictWriter(f1, emoList)
dict_writer.writerow(emoList)
我想将以下值附加到已创建的csv文件中。我的意思是dict值应该附加到csv的每一行,除了标题值如下。
{''愤怒':0.15,'厌恶':0.0,'恐惧':0.05,'快乐':0.06,'悲伤':0.24,'惊喜':0.02,'中立':0.49}]
我也在分享我的屏幕截图csv file screenshot
这是将列附加到现有CSV的基本示例:
data.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)
data.csv(final)
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