jliu66 0 Light Poster

Hi, My friends,

I have aquestion to ask you. I want to output a list of class objects to a file in csv format.
but last step in writer.writerow does not work. Can you tell me what's wrong and how should I fixed? I want to output each object of the class in a row.
thanks so much in advance.

import csv, sys, os

class MyData(object):
def _init_(self):
self.Age = 0
self.No = 0
pass
data = []

for i in range(2):
tem = MyData()
tem.Age = i*6
tem.No = i*10

data.append(tem)

fid = open('testCSVOut.csv', 'w')
if fid == -1:
# could not find the file
error('File: testCSVOut.csv not found or could not be opened')
writer = csv.writer(fid)
for x in data:
writer.writerow(x)
fid.close

John

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.