I'm need for a help, very new with python. My question is, I have one csv file that i need to insert into database, what if in csv file there is columns don't have any value.
Name, Date, Class Ali,,, Fared,20191015,A
when i run my python, it will not insert because no data.
with open('users.csv', 'r') as f:
reader = csv.reader(f, delimiter =',')
line_count = 0
next(reader) # Skip the header row.
for row in reader:
print (row)
cur.execute( "INSERT INTO test (name,dates,class_type) VALUES (%s,%s,%s)",row )
for row in reader:
This give you a list in row attribute, to access the elements you will have to access via index for eg row[0], row[1]. . .
I've added the following code for your reference::
sql = "INSERT INTO customers (name, dates, class_type) VALUES (%s, %s, %s)"
val = (row[0], row[1], row[2])
cur.execute(sql, val)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.