如何从Excel中以特定格式提取数据如何存储在数据库中 [英] How to extract data from excel in specific format & how to store in Database

查看:72
本文介绍了如何从Excel中以特定格式提取数据如何存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面显示的excel文件,我需要使用python提取不同格式的数据。因此,请帮助我尝试以下代码:

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)

我使用上面的代码单击以查看但无法获得期望输出

期望输出应在以下列表中:[学生年龄,1990年,20岁],[学生年龄,1991年,21岁],[学生年龄,22岁, 1992],......整版。使用for循环。
excel工作表是:单击以查看

因此,请帮助...。等待响应...

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...

推荐答案

您的要求与Excel工作表中的数据相矛盾。我发现的一个问题是,您现在正在清除每行之后的列表。.

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

这篇关于如何从Excel中以特定格式提取数据如何存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆