-5
我的文本文件看起來像這樣:如何使用Python將空格分隔的文本文件插入到mysql中?
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
如何可以解析並插入這些單獨的行插入我的數據庫?
DB結構:(日期,版本,網址,名稱,描述)
我要插入從文本文件中的值使用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
這不是人們爲你做事情的論壇。付出一些努力。你嘗試了什麼? – Cfreak 2013-05-11 05:43:31
請再次檢查我的帖子。我發佈了迄今爲止所做的編碼。我是新手,但必須完成這一點。任何幫助將不勝感激。 – aki2all 2013-05-11 06:45:51