![](/img/trans.png)
[英]I've created a python script to import excel sheets into my DB, But now i want create Gui which will take file path as input
[英]I've written the python code to import excel file (multiple sheets) into my sql server, But for large file it taking too much time
我正在尝试将带有多张工作表的大型 excel 文件导入我的 sql 服务器,但问题是它花费了太多时间,有什么办法可以提高效率或以更好的方式做到这一点。我有点新这种语言,所以任何帮助都将不胜感激。这是我的代码:
#taking file input and reading it
filename = input("Input the Filename: ")
dfs = pd.read_excel(filename, sheet_name=None)
#my main function
d = {k: v[['SR_NO', 'NTN']].values.tolist()
for k, v in pd.read_excel(filename, sheet_name=None).items()
}
for k, v in dfs.items():
cols = ['SR_NO', 'NTN']
dfs = pd.read_excel(filename, usecols=cols, sheet_name=None)
records = pd.concat(df for df in dfs.values()).to_numpy().tolist()
d[k] = records
#for test sheetnames
print (d.keys())
#cursor connection to insert records
try:
cursor = conn.cursor()
cursor.executemany(sql_insert, records)
cursor.commit();
except Exception as e:
cursor.rollback()
print(str(e[1]))
finally:
print('Task is complete.')
cursor.close()
conn.close()
我可能误解了你的意图,但我怀疑你所需要的就是这个。 读取文件一次。 并且for
循环已经通过 dfs 值进行枚举,因此您不需要在内部进行另一个循环。 而且你不需要concat
,如果你只有一件事。
filename = input("Input the Filename: ")
dfs = pd.read_excel(filename, usecols=['SR_NO','NTN'], sheet_name=None)
d = {}
for k, v in dfs.items():
d[k] = v.to_numpy().tolist()
print (d.keys())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.