简体   繁体   中英

how to write data row wise in csv file by following code?

I am getting an error while running the following code, how do I fix it?

Error:

csv.Error: iterable expected, not 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,:])

Try this instead employee_writer.writerows(list(y_test[num,:]))

You should make y_test[num,:] iterable, modifying it to be a sequence of lists you can replace y_test[num,:] with map(lambda x: [x], y_test[num,:]) . Another way is instead of making a NumPy array y_test , making an iterable out of the test_labels data frame using to_csv() method from pandas Series from scratch.

You can call writerows directly on the array:

employee_writer.writerows(y_test)

You can just do this:

with open('/content/drive/My Drive/GEINet_and_PEINet/VGG_CSV/test.csv', mode='w') as employee_file:
    employee_writer.writerows(y_test)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM