【问题标题】:Load multiple excel sheets into mysql将多个excel工作表加载到mysql中
【发布时间】:2019-05-14 04:55:52
【问题描述】:

我有多个 excel 文件,其中有多个工作表我正在尝试将它们加载到 mysql 数据库中 以下是我在一张纸上的 excel 工作代码

import pandas as pd
    df = pd.read_excel(open(path+ "/" +file, 'rb'), sheet_name='Sheet1')
                      table_name = "sample"
                      # Defaulting null values to 0 .
                   df=df.fillna(0)
                 # inserting the data.
                   df.to_sql(con=engine, name=table_name, if_exists='replace', schema=None)

上面的代码有效,但有一个问题 1. 我对表名进行硬编码,理想情况下我希望与 excel 文件具有相同的名称,我可能可以使用 split 并只获取文件名是否有更好的方法来获取不带扩展名的文件名。

但真正的问题在这里

现在在我的文件夹中可以有多个 excel 文件,其中包含多个工作表 示例 document1.xlsx(其中有两张表 sheet1 和 sheet2) 这就是我所做的

    xls = pd.ExcelFile('document1.xlsx')
                sheets = []
                sheets = xls.sheet_names
                #type(sheets)
                #print(sheets)this gives me list containing sheet1,sheet2
                for i in sheets:
                    #print(i) 
                    df = pd.read_excel(open(path+ "/" +file, 'rb'), sheet_name=i)
   df.to_sql(con=engine, name=table_name, if_exists='replace', schema=None)

在上面的代码中,数据框保存了两个工作表数据,但我想存储工作表 1 数据,首先将其加载到一个表中,然后取出第二个工作表并将其加载到另一个表中,所以在上面的 df 中我做了这个更改看看代码是否正常工作 pd.read_excel(open(path+ "/" +file, 'rb'), sheet_name=i[0]) 但它没有任何想法?

谢谢

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这绝对不是 Python 的答案,但如果您可以使用其他工具,我建议您这样做。

    https://www.rondebruin.nl/win/addins/rdbmerge.htm

    使用插件,将所有文件(文件夹中)的所有工作表合并到一个主数据集中。然后,将其推送到 MySQL,或使用 Workbench 将其导入 MySQL。很高兴看到 Python 这样做,但如果您正处于时间紧迫的状态并且只想完成这件事,请尝试我在此处推荐的方法。

    【讨论】:

    • 您好,感谢您的解决方案,感谢您的努力。我设法使用 python pandas 和 sql alchemy 得到了这个。我现在没有代码,但这是方法。首先使用 pandas Excelfile 选项打开完整的 excel 接下来是使用 sheet_names 将工作表名称放入列表 第 3 步将使用 pandas read_excel 并将工作表名称传递给 sheet_name 参数并使用 df_to_sql pandas 函数/方法(对不起,python 新手)和加载到mysql数据库。希望这很清楚,我可能会在晚上发布代码。再次感谢您的努力。
    • 这是有道理的。如果您有机会可以发布代码的最终版本,那就太好了。一方面,我很想看到解决方案。我很确定其他人也会有兴趣看到这个。整个社区都将从中受益。
    • 另请注意,如果excel太大或太宽,那么我们可能需要将excel转换为csv,然后将其加载到mysql数据库中。谢谢。
    【解决方案2】:

    @ryguy72 代码看起来像这样

    xls = pd.ExcelFile(path + "/" + file)
                      #Create a list which consists of all sheet names in a Excel file.
                      sheets = []# declaring empty list
                      sheets = xls.sheet_names # getting sheet names
                      ex_op = open(path +"/" + file, 'rb')# opening the Excel sheets
                      for i in sheets:
                          # Passing the sheet names as table names.
                          table_name = i
                          #read that sheet that is being processed
                          df = pd.read_excel(ex_op, sheet_name=i)
                          # Defaulting null values to 0 to be confirmed.
                          df=df.fillna(0)
                          #Droping and recreating the table and inserting the data.
                          df.to_sql(con=engine, name=table_name, if_exists='replace', schema=None)
                      # Close the Excel file.
                      ex_op.close()
    

    这段代码满足我的要求,可以编辑它来做很多其他事情。

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 2021-11-21
      • 2020-05-23
      • 2011-09-26
      • 1970-01-01
      相关资源
      最近更新 更多