【发布时间】:2017-11-27 23:10:45
【问题描述】:
我正在编写用于从存档 CSV 插入表“noticias”信息的代码,但它不起作用,当我调试时,此错误显示:
Connected to pydev debugger (build 171.4694.38)
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.4\helpers\pydev\pydevd.py", line 1591, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.4\helpers\pydev\pydevd.py", line 1018, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/SoriyAntony/PycharmProjects/cnnbd/cnnbd", line 11, in <module>
cursor.execute("INSERT INTO noticias(title, link, pubDate) \ VALUES({1},{2},{3})" % tuple(list))
TypeError: not all arguments converted during string formatting
Process finished with exit code 1
这是存档 cnn.csv:
title,link,pubDate
Hundreds of households to be evacuated in wake of London tower fire,http://www.cnn.com/2017/06/23/world/london-fire-safety-evacuations/index.html,"Fri, 23 Jun 2017 20:22:02 GMT"
"Ex-Obama official: Handling of Russia 'is hardest thing to defend,' WaPo reports",http://www.cnn.com/collections/intl-wapo-former-obama-official/,"Fri, 23 Jun 2017 17:47:54 GMT"
"Saudi Arabia, major combatant in Yemen, to tackle spread of cholera",http://www.cnn.com/2017/06/23/middleeast/yemen-saudis-cholera/index.html,"Fri, 23 Jun 2017 18:29:37 GMT"
Violent homophobia festers in Erdogan's shadow,http://www.cnn.com/2017/06/23/europe/turkey-homophobia-violence/index.html,"Fri, 23 Jun 2017 13:11:52 GMT"
Qatar given 10 days to comply with 13 demands from Arab states,http://www.cnn.com/collections/qatar-intl/,"Fri, 23 Jun 2017 11:41:52 GMT"
Al Jazeera: What you need to know,http://www.cnn.com/videos/cnnmoney/2017/06/23/al-jazeera-explainer-mxb-lon-orig.cnnmoney,"Fri, 23 Jun 2017 16:01:18 GMT"
这是代码:
import mysql.connector
import pandas as pd
cnx = mysql.connector.connect(user='root', password='password', host='localhost', database='cnn')
cursor = cnx.cursor()
csv_data = pd.read_csv('cnn.csv')
for row in csv_data.iterrows():
list = row[1].values
#cursor.execute("""INSERT INTO noticias (title, link, pubDate) VALUES('%s', '%s', '%s');""")
cursor.execute("INSERT INTO noticias(title, link, pubDate) VALUES({1},{2},{3})" % tuple(list))
cursor.close()
cnx.close()
我正在测试两行关于 curso.execute 和第一行,调试未检测到错误但未写入表,第二行标记此处解释的错误。 关于这个错误的任何想法,我已经安装了 python 3.6 和 mysql 5.7
这是表格公告:
mysql> describe noticias;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| title | varchar(500) | YES | | NULL | |
| link | varchar(500) | YES | | NULL | |
| pubDate | varchar(500) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
【问题讨论】:
-
你能打印变量列表看看你得到了什么