簡體   English   中英

我已經編寫了 python 代碼以將 excel 文件(多張)導入我的 sql 服務器,但是對於大文件,它需要太多時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM