如何通过以下代码在csv文件中逐行写入数据?

问题描述 投票:0回答:2

运行以下代码时出现错误,该如何解决?

错误:

csv.Error:可迭代,不是numpy.int64

y_test = np.array(test_labels)
print('y_test_labels:', y_test.shape) # (230,123)

with open('/content/drive/My Drive/GEINet_and_PEINet/VGG_CSV/test.csv', mode='w') as employee_file:
    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    #employee_writer.writerow(y_test)
    x,y = y_test.shape
     print('x: ',x)
    for num in range(0, x):
        employee_writer.writerows(y_test[num,:])
python csv
2个回答
0
投票

尝试此方法employee_writer.writerows(list(y_test[num,:]))


0
投票

您应该使y_test[num,:]迭代,将其修改为可以用y_test[num,:]替换map(lambda x: [x], y_test[num,:])的列表序列。另一种方法是代替NumPy数组y_test,而使用pandas test_labels中的to_csv()方法从Series数据帧中迭代出来。

© www.soinside.com 2019 - 2024. All rights reserved.