user1681664
user1681664

Reputation: 1811

saving arrays in an array to csv file using python

I am trying to save arrays into a csv file using python.

I have an array of arrays:

myarr = [[10.3,11.2,10.7],[13.4,12.6,12.7],[12.56,14.21,11.33]]

I would like to save it into a csv file into the following format:

    A      B      C
1  10.3   11.2   10.7
2  13.4   12.6   12.7
3  12.56  12.41  11.33

I need it to save without deleting previous rows, so if I go back into the same file to try and save more arrays, it shouldn't delete the old rows, it should start at the next available row.

Thank you for your help.

Upvotes: 2

Views: 5950

Answers (1)

Nilani Algiriyage
Nilani Algiriyage

Reputation: 35626

I have no idea of what these column headers are. Please explain it more. You can use following code to write the array in to a csv file row by row.

import csv
myarr = [[10.3,11.2,10.7],[13.4,12.6,12.7],[12.56,14.21,11.33]]
with open("myarray.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(myarr)

Upvotes: 4

Related Questions