社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

kumarm

kumarm 最近创建的主题
kumarm 最近回复了
6 年前
回复了 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()

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