以下代码读取CSV文件,选择GLTGV列,并将日期从美国转换为英国。但是它给我一个错误AttributeError: 'Series' object has no attribute 'iterrows'
。我尝试将其替换为iteritems
,但未成功。谁能帮我解决迭代问题或提出替代解决方案?
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()