Python从excel创建sqlite3数据库;但是它将数据库名称创建为"";database.x


excel_file = self.filename
con = sqlite3.connect(excel_file + ".db")
wb = pd.read_excel(excel_file, sheet_name = [0])
for sheet in wb:
wb[sheet].to_sql(sheet, con, index=False)
con.commit()
con.close()

嗨,上面是一个函数,当我点击"上传名册"按钮(tk按钮(时会调用它。它完美地将数据更新到sqlite3数据库中;然而,问题是它将数据库文件创建为"nameofdatabase.xlsl.db",而不是"nameofdatabase.db"。此外,由于数据库中有一个表,默认情况下,它将表名设置为"0"(零(。有什么建议吗?

使用os.path.splitext:

import os
con = sqlite3.connect(os.path.splitext(excel_file)[0] + ".db")

最新更新