the following code reads the CSV file, pick the column GLTGV and converts the date from the US to UK. However it gives me an error AttributeError: 'Series' object has no attribute 'iterrows'
. I tried to replace it by iteritems
, but unsuccessfully. Can anyone help me with iteritems or propose alternate solution?
import pyodbc
import pandas
import os
import sys
import struct
try:
currdir = os.path.abspath(__file__)
except NameError:
import sys
currdir = os.path.abspath(os.path.dirname(sys.argv[0]))
serverpath = os.path.join(currdir, 'serverlink.txt')
f=open(serverpath,"r")
lines=f.readlines()
servername=lines[0]
f.close()
print(servername)
serv = servername
datab = 'BACKEND'
cnxn = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = serv, database = datab)
import fileinput
import os
import csv
import pandas as pd
cwd = os.getcwd()
directory = (cwd + '\\')
for file in os.listdir(directory):
if file.endswith( "USR02_FINAL.csv"):
data = pd.read_csv(directory + "USR02_FINAL.csv", sep=",", usecols=['GLTGV'], low_memory=False, encoding='latin-1')
data = pd.to_datetime(data['GLTGV'], dayfirst=True, errors='coerce').dt.strftime("%d/%m/%Y").fillna("")
print(data)
cursor = cnxn.cursor()
for index,row in data.iterrows():
cursor.execute("INSERT INTO dbo.USR_02_ALL([GLTGV]) values(?)", row(['GLTGV']))
cnxn.commit()
cursor.close()
cnxn.close()