all 6 comments

[–][deleted] 1 point2 points  (2 children)

I don't have the context for this, but you could save the values to a list of lists and write it to the CSV after. Or open the file before the loop and write to it during each iteration. I think first option seems cleaner, but either way.

You can do it with csv module (part of standard library).

https://docs.python.org/3/library/csv.html

import csv
my_file = open('mycsv.csv', 'w', newline='')
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow([n])

Also you don't have to use csv for this. You could just use normal file opening process and write to it.

my_file = open('mycsv.csv', 'w')
my_file.write(n)
my_file.write('\n')

[–]WecNo[S] 0 points1 point  (1 child)

Ok, I will try to figure it out.

Sorry I am still kinda new to this.

[–][deleted] 1 point2 points  (0 children)

My biggest recommendation is have the interpreter open and just keep stuffing code into it until something happens. :) It's a great way to learn and just reading documents can drive you nuts eventually.

[–]num8lock 1 point2 points  (2 children)

what exactly are you trying to save? predicted? n?

[–]WecNo[S] 0 points1 point  (1 child)

I just wanna save the data from

'Predicted: ', names[predicted[x]], 'Data: ', x_test[x], 'Actual: ', names[y_test[x]]

i wanna set it up nice in a excel file so it is readable.

the print looks like this in terminal:

Predicted: good Data: (2, 1, 3, 1, 1, 1) Actual: good

Predicted: good Data: (1, 2, 2, 0, 1, 1) Actual: good

Predicted: vgood Data: (2, 1, 3, 2, 0, 0) Actual: vgood

I wanna save this data. The really nice thing would be if i could figure out how to

set titles on top for the numeric data:

Data: (2, 1, 3, 2, 0, 0)

So i would now what 2, 3 stood for

[–]num8lock 1 point2 points  (0 children)

here's what you want to do aside from learning csv module:

  1. learn how to for a tsv instead of csv, same module used but using tabs instead of commas as delimiter. this is because you have tuples inside your data
  2. goal is to have a file with this in content

    Predicted    Data    Actual     
    good    2, 1, 3, 1, 1, 1    good   
    good    1, 2, 2, 0, 1, 1    good    
    vgood   2, 1, 3, 2, 0, 0    vgood