我有一个CSV文件,其中包含Registrationdate列。其格式为月/日/年。我正在尝试读取这个CSV并将数据插入数据库。但它显示了一个日期格式错误。我知道SQL的数据格式是年月日。但是,在将整个列插入数据库之前,我需要一种方法将其转换为这种格式。请告诉我一种将格式从mm/dd/y转换为y-mm-dd的方法。
import csv
with open('STUDENTDATA.csv',newline='',encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
sql = "INSERT INTO STUDENTDETAILS (studentid, firstname, lastname, registrationdate, class, section) VALUES ('%s','%s','%s','%s','%s','%s')" % (row[0],row[1],row[2],row[3],row[4],row[5])
try:
cursor = mydb.cursor()
cursor.execute(sql)
print("Value inserted!")
mydb.commit()
except Exception as e:
print(str(e))
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
1292 (22007): Incorrect date value: '1/13/2021' for column 'REGISTRATIONDATE'
at row 1
*如果您从数据集中提供一些数据,情况会更清楚。
否则,请尝试转换日期格式,并让我知道它是否有效
#编辑
以下是我们如何将日期转换为";YYYY-MM-DD";
data=[] #contains all the data from your dataset
formated_date=[] #contains formated date in the form of "YYYY-MM-DD"
with open('STUDENTDATA.csv',newline='',encoding='utf-8') as csvfile: #reading the csv file
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
data.append(row)
for i in data:
a=i[3].split('/')
newform=f'{a[2]}-{a[0]}-{a[1]}' #changing the date format
formated_date.append(newform) #and saving it to another list
我们在这里所做的是,我们将数据集中的所有数据收集到一个列表中,即"data",我们解析出包含日期的部分,并用"/"将其拆分,然后我们添加了一个新的列表,即"formated_date",其中包含sql接受的日期格式的格式化日期。
现在我们需要将代码与sql连接起来,并将其保存在数据库中。
#这是的最终代码
import csv
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='*******',passwd='*****',database='STUDENTDATA')
cursor=mydb.cursor()
data=[] #contains all the data from your dataset
formated_date=[] #contains formated date in the form of "YYYY-MM-DD"
with open('STUDENTDATA.csv',newline='',encoding='utf-8') as csvfile: #reading the csv file
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
data.append(row)
for i in data:
a=i[3].split('/')
newform=f'{a[2]}-{a[0]}-{a[1]}' #changing the dateformat
formated_date.append(newform) #and saving it to another list
for i in range(len(data)):
sql="INSERT INTO STUDENTDETAILS (studentid, firstname, lastname, registrationdate, class, section) VALUES ('%s','%s','%s','%s','%s','%s')" % (data[i][0],data[i][1],data[i][2],formated_date[i],data[i][4],data[i][5])
try:
cursor = mydb.cursor() #saving the file into the database
cursor.execute(sql)
print("Value inserted!")
mydb.commit()
except Exception as e:
print(str(e))
希望它能有所帮助。还有什么问题请告诉我。