1

I need to write the following values from a list to a CSV.

my_list[0]
March,'1.800931853','1.843227636','1.861992226','1.880196477','1.907555762','1.853076585','1.770560438','1.837108443','1.867213313','1.849542335','1.913505573','2.023772836','1.899717681'

my_list[1]
July,'10.809832','10.57868801','9.571741038','8.850265221','9.154250034','9.172628705','10.35004201','9.36852657','8.689944331','10.1053826','9.18756731','8.541507801','8.054240996'

I have tried the following:

def saveData(my_list):
    file_name = 'months.csv'
        with open(file_name, 'w') as f:
            writer = csv.writer(f)
            for row in zip(my_list):
                writer.writerows([row])
        f.close()

The CSV should look like this with each value in a separate cell

March 1.800931853 1.843227636 1.861992226 1.880196477 1.907555762 1.853076585 1.770560438 1.837108443 1.867213313 1.849542335 1.913505573 2.023772836 1.899717681 2.152980753

1 Answers1

3

This works with Python 3:

import csv

def save_data(my_list):
    file_name = 'months.csv'
    with open(file_name, 'w') as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_NONE, delimiter=' ')
        for row in my_list:
            wr.writerow(map(str, row))

#example:
save_data([["March", 1.2, 1.5], ["July", 2.3, 4.5678, 9.101]])