简体   繁体   中英

insert csv file mysql with date time error

I am trying to insert csv file into mysql but I have error in my datetime field. I tried this but recive this error on this line..... date,time = startdatetime.split('/') ValueError: need more than 1 value to unpack mycsv file rows are like these:

2880,HY13KZV,2014/07/07 09:34:28,2014/07/08 09:34:28,1280
2880,RK09UZB,2014/07/07 10:34:05,2014/07/07 16:34:05,640
for row in reader:
                print row
                try:
                    (machine_name,vrm,startdatetime,enddatetime,ticket_price) = [x.decode('utf-8-sig') for x in row]
                except:
                    print "Error with row: " % row
                tmp = startdatetime.split(" ")
                tmp = enddatetime.split(" ")
                vrm = vrm.replace(' ','')
                vrm = vrm.upper()

                ticket_price = int( SessionCost / 100)

                date,time  = startdatetime.split('/')
                month,day,year = startdate.split('/')

                entryDatetime = "%s-%s-%s %s" % (year,month,day,time)

                date,time = enddatetime.split('/')
                month,day,year = enddate.split('/')

                expiryDatetime = "%s-%s-%s %s" % (year,month,day,time)

                sql_local = """INSERT INTO customer_1.pay_and_display
                    (plate, machine_id, ticket_datetime, expiry_datetime, ticket_name, ticket_price)
                    VALUES ("%s", "%s", "%s", "%s", "%s", "%s") """ % (vrm, machine_name, entryDatetime, expiryDatetime, "Ringo", SessionCost)
            print sql_local
            cursor.execute(sql_local)

            curl = pycurl.Curl()
            body = Body()

Your problem is splitting on "/" brings out 3 fields

year,month,day  = startdatetime.split('/')

Actually there is a bit more errors than that. Use the following in its place

date,time = startdatetime.split(' ')
year,month,day = date.split('/')
hour,min,second = time.split(':')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM