How to iterate over a list of objects and generate an excel file out of those objects?

Viewed 153

I'm trying to produce an excel file containing records of the objects in a list. I get a file with only the last record. It seems that the records over write each other. Here is my code:

import pandas as pd 
class dog:
    def __init__(self, id,type, name, age):
        self.id = id
        self.type = type
        self.name = name
        self.age = age
dogs = []
for i in range (10):
    newDog = dog(i, 'any', 'any2', i+1)
    dogs.append(newDog)

fname = "StatisticsDogs.xlsx"
writer = pd.ExcelWriter(fname, engine='xlsxwriter')
for d in dogs:
    df = pd.DataFrame({'dog ID':[d.id], 'dog NAme':[d.name]})
    df.to_excel(writer, sheet_name='Sheet1')
writer.save()

Thanks in advance.

3 Answers

the answer is:


import pandas as pd 
class dog:
    def __init__(self, id,type, name, age):
        self.id = id
        self.type = type
        self.name = name
        self.age = age
dogs = []
for i in range (10):
    newDog = dog(i, 'any', 'any2', i+1)
    dogs.append(newDog)
lisIDs = []
lisNames = []
for d in dogs:
    lisIDs.append(d.id)
    lisNames.append(d.name)
fname = "StatisticsDogs.xlsx"
writer = pd.ExcelWriter(fname, engine='xlsxwriter')
for d in dogs:
    #df = pd.DataFrame({'dog ID':[d.id], 'dog NAme':[d.name]})
    df = pd.DataFrame({'dog ID':lisIDs, 'dog NAme':lisNames})
    df.to_excel(writer, sheet_name='Sheet1')
writer.save()

Hello there you could also use the python xlwt library: https://pypi.org/project/xlwt/ to create an excel file to store the dog information

Here is an example of how you could potentially do such a thing:

import xlwt
from xlwt import Workbook

class dog_type:
    def __init__(self, id, type, name, age):
        self.id = id
        self.type = type
        self.name = name
        self.age = age
        filename = "put filename here.xls"
        sheetname = 'sheet name'
        wb = Workbook()
        sheet = wb.add_sheet(sheetname)
        sheet.write(1, 0, id)
        sheet.write(2, 0, type)
        sheet.write(3, 0, name)
        sheet.write(4, 0, age)
        wb.save(filename)

dog_type('dog id', 'dog type', 'dog name', 'dog age')

You are essentially using a Workbook to add the proper information into your excel spreadsheet. The Workbook function is xlwt's way of storing information similar to how pandas pd.DataFrame function serves the purpose of storing your dog identification. here is some documentation to understand more what it does in a more detailed fasion: https://xlwt.readthedocs.io/en/latest/api.html

Here is one way to do it using xlsxwriter:

import xlsxwriter

class dog:
    def __init__(self, id,type, name, age):
        self.id = id
        self.type = type
        self.name = name
        self.age = age

dogs = []
for i in range (10):
    newDog = dog(i, 'any', 'any2', i+1)
    dogs.append(newDog)


workbook = xlsxwriter.Workbook('StatisticsDogs.xlsx')
worksheet = workbook.add_worksheet()

for row_num, dog in enumerate(dogs):
    worksheet.write(row_num, 0, dog.id)
    worksheet.write(row_num, 1, dog.name)
    worksheet.write(row_num, 2, dog.age)

workbook.close()

Output:

enter image description here

Related