0

I want to append a list of values at the end of each row of a csv file except the header.

I have tried following code but it deletes all the rows from csv file and does not appends any value to the csv file.

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)

I want to append the following values to a csv file which is already created. I mean the dict values should be appended to each row of csv except the header values like below.

{'angry': 0.15, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.06, 'sad': 0.24, 'surprise': 0.02, 'neutral': 0.49}

I am also sharing the screenshot of my csv file screenshot

6
  • Please reformat the code above which wouldn't run as no indentations are present. Commented Feb 29, 2020 at 13:45
  • edited please check Commented Feb 29, 2020 at 13:51
  • Hi, I have formatted the code and added the imports as well, please check, thank you Commented Feb 29, 2020 at 13:57
  • Please help, I have updated the requested edits Commented Feb 29, 2020 at 14:12
  • every time the for loop iterates, it gives the values something like {'angry': 0.15, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.06, 'sad': 0.24, 'surprise': 0.02, 'neutral': 0.49} Commented Feb 29, 2020 at 15:26

1 Answer 1

0

Here's a basic example of appending columns to an existing CSV:

data.csv (original)

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I appreciate your help but, please refer to my given code ; every time the for loop iterates, it produces emolist = {'angry': 0.15, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.06, 'sad': 0.24, 'surprise': 0.02, 'neutral': 0.49} every time the values of angry , disgust , fear, happy , sad , surprise and neutral changes , now I don't want to update output.csv with input.csv , I want to update the output.csv with the emolist that is generated in every for loop iteration
@Yasir Then replace the hard-coded emolist in my example with your code to generate the value. You didn't provide working code for that so just gave an example. If you want to read change the file in place, you'll have to read the whole file into memory, process the lines, then write the whole file out to the same filename. I'll update the example for writing to the same file.
Thanks @Mark I tried replacing the emolist in your code with the emoList variable of my code but it makes the output file empty. Please update the code to take values from emoList variable and append it to the end of each row of my output csv file, Thanks a million

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.