我有一个很大的数据集,在第二列有句子和他们的情绪状态。我开发了代码来将它们读作numpy数组。我需要的是,如果一个句子的情感是中性的,那么返回为真,否则返回假。if-else条件返回的每个结果都应写入CSV文件。但是这里它只在CSV文件中写入一次结果,而不是写入for循环每次迭代返回的所有结果。以下是我目前掌握的代码。在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'})`
我可能在某个地方错了。谁来帮帮我。在