2013-03-08 88 views
0

我有代碼:類型錯誤:沒有足夠的參數格式字符串

with open('D:\\Downloads\\Train_rev1.csv', 'rb') as csv_file: 
    data = csv.reader(csv_file, delimiter=',') 
    data.next() 
    records = ",".join('({}, {})'.format(row[0], row[1]) for row in data) 
print 'inserting records into {} ...'.format('train_rev1') 
result = conn.execute("INSERT INTO train_rev1 (id, title) VALUES {}".format(records)) 
print 'Records inserted into {} {}'.format('train_rev1', 'successful' if result else 'unsuccessful') 

和我

> inserting records into train_rev1 ... Traceback (most recent call 
> last): File "I:\test\IceSugar\test.py", line 81, in <module> 
>  insert_trainRev1Records(conn) File "I:\test\IceSugar\test.py", line 54, in insert_trainRev1Records 
>  result = conn.execute("INSERT INTO train_rev1 (id, title) VALUES {}".format(records)) File 
> "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py", 
> line 1449, in execute 
>  params) File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py", 
> line 1628, in _execute_text 
>  statement, parameters File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\base.py", 
> line 1691, in _execute_context 
>  context) File "C:\Python27\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\engine\default.py", 
> line 331, in do_execute 
>  cursor.execute(statement, parameters) File "C:\Python27\lib\site-packages\mysql_python-1.2.4c1-py2.7-win32.egg\MySQLdb\cursors.py", 
> line 183, in execute 
>  query = query % db.literal(args) TypeError: not enough arguments for format string 

我不知道,什麼是錯的?

+2

該代碼不會引發該錯誤。這是你的確切代碼嗎?充其量,我期望一個IndexError。 – 2013-03-08 17:07:24

+2

請發佈完整的回溯(和更多的代碼)。 – 2013-03-08 17:07:41

+0

如果數據是正確的,這應該工作,像[[1,2],[3,4]]? – jurgenreza 2013-03-08 17:12:10

回答

1

你的代碼似乎沒什麼問題:

>>> data = [[1,2],[3,4]] 
>>> records = ",".join("({0}, {1})".format(row[0], row[1]) for row in data) 
>>> records 
'(1, 2),(3, 4)' 

假設當然是data有我推斷格式。

所以我的猜測是你有一個問題,你的data對象的格式,或者你沒有發佈實際的代碼。

相關問題