【发布时间】:2013-05-05 20:10:49
【问题描述】:
我的文本文件如下所示:
10May2013 3.1 http://ncbi.org p.sojae_aafeatureimp p.sojae_aafeatureimp
10May2013 3.2 http://ncbi.org p.sojae_aasequenceimp p.sojae_aasequenceimp
10May2013 3.3 http://ncbi.org p.sojae_blatalignment p.sojae_blatalignment
如何解析这些单独的行并将其插入到我的数据库中?
数据库结构:(日期、版本、网址、名称、描述)
我必须使用 Python 将文本文件中的值插入 mysql。到目前为止,我的脚本是这样的:
import MySQLdb, csv, sys
with open('externaldatabase.txt') as textfile:
csvreader = csv.reader(textfile)
csvdata = []
for row in csvreader:
csvdata.append(row)
conn = MySQLdb.connect (host = "localhost",user = "username", passwd = "password",db = "database_name")
c = conn.cursor()
for row in csvdata:
# Insert a row of data
c.execute("INSERT INTO externaldatabase (release_date, version, download_url, name, description) VALUES ('%s', '%s', '%s', '%s', '%s')" % (row))
conn.commit()
c.close()
但它让我出错:
Traceback (most recent call last):
File "csv1.py", line 16, in <module>
c.execute("INSERT INTO externaldatabase (release_date, version, download_url, name, description) VALUES ('%s', '%s', '%s', '%s', '%s')" % (row))
TypeError: not enough arguments for format string
【问题讨论】:
-
这不是人们为您做事的论坛。付出一些努力。你尝试了什么?
-
请再次查看我的帖子。我发布了到目前为止我所做的编码。我是新手,但必须完成这个。任何帮助将不胜感激。