0

I am trying to fetch the data from csv and insert it into mysql table .While trying that I am getting the Syntax Error . Please help me to sort out this error .

code

import csv
import MySQLdb as mdb
import ast
cr = csv.reader(open("tushar.csv","rb"))
k=0
con = mdb.connect('***', '***', '***', '****')
cur = con.cursor()
for row in cr:
    if k%2==0:
        t=row
        print t
    else:
        l1=row
        l=ast.literal_eval(l1[0])
        sql="INSERT INTO amazon_order  (orderno,mainStatus,stateCode,timeStamp,destZip,orderDate,cost) Values('%s','%s','%s')"%(t,l["status"],l["state_code"],l["processed_timestamp"],l["destination_zip"],l["odr_date"],l["cost"])
        print sql
        cur.execute(sql)
        con.commit()
    k+=1
if con:
    con.close()     

Error

['497832']
Traceback (most recent call last):
  File "linkedlist.py", line 14, in 
    l=ast.literal_eval(l1[0])
  File "/usr/lib/python2.7/ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "", line 1
    {"status": "All items processed"
                                   ^
SyntaxError: unexpected EOF while parsing

tushar.csv

497832
{"status": "All items processed", "state_code": "GA", "processed_timestamp": "2013/05/14 19:32:08 UTC", "destination_zip": "31028", "odr_date": "2013-05-13 00:00:00""cost": 54.08}
487870
{"status": "All items processed", "state_code": "CT", "processed_timestamp": "2013/03/11 22:15:43 UTC", "destination_zip": "06468", "odr_date": "2013-03-11 00:00:00","cost": 149.43}
and so on..
1
  • 1
    Your data is truncated, it appears. Commented Jul 12, 2013 at 12:45

1 Answer 1

0

You shouldn't be using CSV. Your file isn't in a comma-separated values format. CSV splits the file lines into unparsable pieces.

Take

{"status": "All items processed", "state_code": "GA", "processed_timestamp": "2013/05/14 19:32:08 UTC", "destination_zip": "31028", "odr_date": "2013-05-13 00:00:00""cost": 54.08}

and split it on the ',' and you get a list:

['{"status": "All items processed"',
 '"state_code": "GA"',
 '"processed_timestamp": "2013/05/14 19:32:08 UTC"',
 (etc..)

That first line is looking kinda familiar.

Just open the file and read it line by line - skip the csv thing entirely.

Sign up to request clarification or add additional context in comments.

Comments

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.