擅长:python、mysql、java
<p>您需要给<a href="http://www.python.org/dev/peps/pep-0249/#executemany" rel="noreferrer">^{<cd1>}</a>一个行列表。你不需要把名字和电子邮件分成单独的列表,只需要创建一个包含两个值的列表。</p>
<pre><code>rows = []
for row in range(sheet.nrows):
"""name is in the 0th col. email is the 4th col."""
name = sheet.cell(row, 0).value
email = sheet.cell(row, 4).value
rows.append((name, email))
db = MySQLdb.connect(host=host, user=user, db=dbname, passwd=pwd)
cursor = db.cursor()
cursor.executemany("""INSERT INTO mailing_list (name,email) VALUES (%s,%s)""", rows)
</code></pre>
<p>更新:正如@JonClements提到的,应该是<code>executemany()</code>而不是<code>execute()</code>。</p>