I have to weekly upload a csv file to sqlserver, and I do the job using python 3. The problem is that it takes too long for the file to be uploaded (around 30 minutes), and the table has 49000 rows and 80 columns.
Here is a piece of the code, where I have to transform the date format and replace quotes as well. I have already tried it with pandas, but took longer than that.
import csv import os import pyodbc import time srv='server_name' db='database' tb='table' conn=pyodbc.connect('Trusted_Connection=yes',DRIVER='{SQL Server}',SERVER=srv,DATABASE=db) c=conn.cursor() csvfile='file.csv' with open(csvfile,'r') as csvfile: reader = csv.reader(csvfile, delimiter=';') cnt=0 for row in reader: if cnt>0: for r in range(0,len(row)): #this is the part where I transform the date format from dd/mm/yyyy to yyyy-mm-dd if (len(row[r])==10 or len(row[r])==19) and row[r][2]=='/' and row[r][5]=='/': row[r]=row[r][6:10]+'-'+row[r][3:5]+'-'+row[r][0:2] #here I replace the quote to nothing, since it is not important for the report if row[r].find("'")>0: row[r]=row[r].replace("'","") #at this part I query the index to increment by 1 on the table qcnt="select count(1) from "+tb resq=c.execute(qcnt) rq=c.fetchone() rq=str(rq[0]) #here I insert each row into the table that already exists insrt=("insert into "+tb+" values("+rq+",'"+("', '".join(row))+"')") if cnt>0: res=c.execute(insrt) conn.commit() cnt+=1 conn.close()
Any help will be appreciated. Thanks!
reader
?\$\endgroup\$