私信  •  关注

kumarm

kumarm 最近创建的主题
kumarm 最近回复了
5 年前
回复了 kumarm 创建的主题 » 将多个Excel工作表加载到MySQL中

@ 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()

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