I am writing the csv file in python and there are four levels of nested objects. like
I need to show the csv like this
StudentName
, StudentCla
Initialize your student.subjects and subject.books with [], and then it'll work. Just as what @user714965 said.
or, use list-comprehension:
for s in [stu if stu.has_key(subjects) for stu in students]:
for subject in [subj if subj.has_key(books) for subj in s.subjects]:
lists= [(s.name, s.class, subject.name, book.name) for book in subject.books]
print r'\n'.join(r','join(lists))
or
if your loop is not very deep,try something like this:
fn= s.has_key('subjects') and (lambda s: /do sth. with s/ ) or (lambda s: /do sth. else with s/)
in your loop, use the fn
like this,
[fn(s) for s in students ]