[英]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
I'm trying to import large excel files with multiple sheets into my sql server but the problem is that it taking too much time, is there any way i can make it efficient or do it in a better way.I'm kinda new to this language so any help can be grateful.Here is my code:我正在尝试将带有多张工作表的大型 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()
I may be misunderstanding your intent, but I suspect all you need is this.我可能误解了你的意图,但我怀疑你所需要的就是这个。 Read the file ONCE.
读取文件一次。 And the
for
loop is already enumerating through the dfs values, so you don't need another loop inside.并且
for
循环已经通过 dfs 值进行枚举,因此您不需要在内部进行另一个循环。 And you don't need concat
, if you only have one thing.而且你不需要
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.