0

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()
1
  • Don't you just use the wrong separator? date,time = startdatetime.split(' ') Commented Jul 9, 2014 at 15:51

1 Answer 1

1

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(':')
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick response but i get the same error date,time = startdatetime.split(' ') ValueError: need more than 1 value to unpack
I am calling this file python ./ringo.py /home/toor/ringolist/RingGoData-2014-06-24.csv
than it could be your row is either blank, or not formatted correctly. So validate that the row is correct at the top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.