简体   繁体   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

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.

相关问题 我创建了一个 python 脚本将 excel 表导入我的数据库,但现在我想创建将文件路径作为输入的 Gui - 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 代码花费了太多时间 - Code is taking too much time 使用 Python 将大型 CSV 文件拆分为单个 Excel 中的多个工作表 - Splitting Large CSV file into multiple sheets in a single Excel using Python sock.recv()花费太多时间在python代码中执行 - sock.recv() is taking too much time to execute in the python code Beautifulsoup 在代码中执行时间过长 - Beautifulsoup taking too much time to execute in the code 编写挑战代码需要太多时间 - Programming Challenge Code taking too much time 文件太大要导入? - File Too Large to import? Python 可执行文件耗时过多 - Python Executable taking too much time 我的谷歌表格文件没有源文件夹,我必须使用 python 将它移动到驱动器文件夹中 - My google sheets file have no source folder , I've to move it into drive folder using python 阅读大字符串并拆分每个单词在python中花费太多时间 - reading large string and split each word taking too much time in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM