0
#!/usr/bin/python
import MySQLdb
import sys
import csv

csvfile='/home/arun/Documents/smartearn/Linux/SA/xbrlparser/UGT-2012-Concepts.csv'
def csvparsing():

    spamReader = csv.reader(open(csvfile, 'rb'), delimiter=',', quotechar='"')

    header=spamReader.next()

    conn = MySQLdb.connect (host = "localhost",
                           user = "arunkamaly",
                           db = "se")



    cursor=conn.cursor()
    for clist in spamReader:
        name=clist[1]+clist[2]
        cursor.execute("""
    INSERT INTO se_xbrl_concepts( xbrl_id,xbrl_name,xbrl_type,Xbrl_enumerations,Xbrl_substitutiongroup,Xbrl_balance, Xbrl_periodtype, Xbrl_IsAbstract,xbrl_label,Xbrl_desc)
     values(clist[0],%s,%s,%s,%s,%s,%s,%s,clist[9],%s)""")(name,clist[3],clist[4],clist[5],clist[6],clist[7],clist[8],clist[10])
     #   print name


    conn.commit()
    cursor.close()


if(__name__=='__main__'):
    csvparsing()

mysql表是

  Field                  | Type         | Null | Key | Default | Extra          |
+------------------------+--------------+------+-----+---------+----------------+
| xbrl_id                | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| xbrl_name              | varchar(255) | NO   |     | NULL    |                |
| xbrl_type              | varchar(10)  | NO   |     | NULL    |                |
| Xbrl_enumerations      | varchar(25)  | NO   |     | NULL    |                |
| Xbrl_substitutiongroup | varchar(255) | YES  |     | NULL    |                |
| Xbrl_balance           | varchar(50)  | YES  |     | NULL    |                |
| Xbrl_periodtype        | varchar(25)  | YES  |     | NULL    |                |
| Xbrl_IsAbstract        | bit(1)       | YES  |     | NULL    |                |
| xbrl_label             | varchar(255) | YES  |     | NULL    |                |
| Xbrl_desc              | varchar(45)  | YES  |     | NULL    |                |
+------------------------+--------------+-----
4

1 回答 1

0

如果没有错误跟踪,很难说这是否是唯一的错误,但这肯定是一个错误:

cursor.execute("""
INSERT INTO se_xbrl_concepts( xbrl_id,xbrl_name,xbrl_type,Xbrl_enumerations,Xbrl_substitutiongroup,Xbrl_balance, Xbrl_periodtype, Xbrl_IsAbstract,xbrl_label,Xbrl_desc)
 values(clist[0],%s,%s,%s,%s,%s,%s,%s,clist[9],%s)""")(name,clist[3],clist[4],clist[5],clist[6],clist[7],clist[8],clist[10])

您需要一个逗号将参数列表与字符串分开,并且您需要将括号移到该对的外部:

cursor.execute("""
INSERT INTO se_xbrl_concepts( xbrl_id,xbrl_name,xbrl_type,Xbrl_enumerations,Xbrl_substitutiongroup,Xbrl_balance, Xbrl_periodtype, Xbrl_IsAbstract,xbrl_label,Xbrl_desc)
 values(clist[0],%s,%s,%s,%s,%s,%s,%s,clist[9],%s)""",(name,clist[3],clist[4],clist[5],clist[6],clist[7],clist[8],clist[10]))
于 2012-06-03T04:45:52.393 回答