1

I have a large data set that has Sentence and their emotion state in the second column. I have developed code to read them as numpy array. Then what I need is, if a sentence's emotion is neutral, then return as true and if not, return false. Each result that return by the if else condition should write in a CSV file. But here it will write the result only one time in the CSV file instead of write all result that return by the each iteration of for loop. Following is the code I have so far.

import csv
import numpy as np

with open('Book1.csv', encoding='utf8') as csvfile:
reader = csv.DictReader(csvfile)

for row in reader:
    text=(row['text'])
    emotion=(row['emotion'])
    my_list=(text, emotion)
    my_array = np.asarray(my_list)

    make_array = (text, "neutral")
    num_array = np.asarray(make_array)

    if np.array_equal(my_array,num_array):

        with open('test.csv', 'w') as csvfile:
            fieldnames = ['result']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

            writer.writeheader()
            writer.writerow({'result': 'True'})

    else:

        with open('test.csv', 'w') as csvfile:
            fieldnames = ['result']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

            writer.writeheader()
            writer.writerow({'result': 'False'})`

I might be wrong in somewhere. Somebody please help me.

3
  • 1
    open('test.csv', 'w') overwrites the file every time it is called. You will need to either open in in append mode open('test.csv', 'a'), or better still, open it only once before the loop. Also, the code under with open('Book1.csv', encoding='utf8') as csvfile: should be indented one level. Commented Apr 30, 2017 at 16:04
  • 1
    oh wow, for every line in book1.csv , you are opening,writing and closing test.csv. Cant you just make a dictionary of required records first and perform write at last..? Commented Apr 30, 2017 at 16:06
  • Any way finally I want to count True and False value. If there is another way rather than that I following. Please tell me. Commented Apr 30, 2017 at 16:14

1 Answer 1

2

You should open the file for append rather than writing, if I understand the problem correctly.

with open('test.csv', 'a') as csvfile:
    fieldnames = ['result']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'result': 'True'})

Also, you are repeating code in the if/else conditional blocks ; you should create a variable result_dict which equals {'result': 'True'} or {'result': 'False'} and then write that outside of the if/else blocks so you don't have to open the file in the conditional blocks. Let me know if this isn't clear.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried for your code. The column name also write repeatly. How can I fix it?
Yes Please. I'm not clear what you said. Let me know how to do it.
What do you mean by column name here?
I mean fieldnames = ['result']
Use a counter in the for loop, to see if you are at 0 or >0. If greater than 0, then no need to write header, otherwise write header.

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.