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
Assuming s.subjects
is None
or some other False
value when there are no subjects, and likewise for books
for s in students:
for subject in s.subjects or []:
for book in subject.books or []:
writer.writerow(s.name, s.class, subject.name, book.name)
More generally, you can write
for s in students:
for subject in s.subjects if else []:
for book in subject.books if else []:
writer.writerow(s.name, s.class, subject.name, book.name)
Where
is whatever expression makes sense