I'm trying to write data from a list of lists to a csv file. This is a simplified version of what I have
class Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightdef get_BMI(self):return (self.weight * self.height) / 42 # this is not how you calculate BMI but let's saymyList = [[Point(30, 183)],[Point(63, 153)]]
Because of the way the data is set up, I store the points in a nested loop. If I wanted to access the first point object’s BMI, I would type
myList[0][0].get_BMI()
I want to write each point's BMI to a CSV (delimited by a comma). How do I do that?
Here's how I thought but it isn't exactly straight forward:
import csv
with open('output.csv', 'w') as csvfile:writer = csv.writer(csvfile)writer.writerows(myList)
It doesn't return any error however it doesn't actually create the CSV file either. Also I want to write the values in myList[i][j].get_BMI()
to file. I don't have a problem with permissions because I use Spyder (python IDE) as root. Right now I'm just running the script through the Spyder console but it should still work and output the CSV file.