How to extract data from excel in specific format & how to store in Database

陌路散爱 提交于 2019-12-13 08:49:22

问题


I have excel file shown bellow from that i need to extract data in different format using python. so please help i tried the bellow code:

import xlrd
book = xlrd.open_workbook("excel.xlsx")

sheet = book.sheet_by_index(0)
year=[]

# for print the year seperately in list. 
for dat in sheet.row_values(0):
     if dat is not '':
        year.append(dat)
print(year)


rowdata = []
for i in range(2, sheet.nrows):
    sheetinlist = sheet.row_values(i)
    for cell in sheetinlist:
             if cell is not '':
                rowdata.append(cell)

print(rowdata)

I print all data without null with the above code Click to see but couldn't get desire output
the desire output should be in list like:[student age, 20, 1990], [student age, 21,1991], [student age, 22,1992],...... for whole sheet. using for loop. The excel sheet is: click to see
So Please help ....Awaiting for response...


回答1:


Your requirement is contradicting the data in your Excel sheet. One problem I can find is that you are now clearing the list after each row ..

    for i in range(2, sheet.nrows):
        sheetinlist = sheet.row_values(i)
        rowdata[:] = []
        for cell in sheetinlist:
                    if cell is not '':
                            rowdata.append(cell)
        print rowData


来源:https://stackoverflow.com/questions/52800392/how-to-extract-data-from-excel-in-specific-format-how-to-store-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!