如何使用Python在mysql中插入空格分隔的文本文件?

2024-06-25 23:14:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我的文本文件如下:

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结构:(日期、版本、url、名称、说明)

我必须使用Python将文本文件中的值插入mysql。到目前为止,我的剧本是:

^{pr2}$

但我错了:

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

Tags: org版本名称数据库httpurldbmysql
1条回答
网友
1楼 · 发布于 2024-06-25 23:14:16

您需要将分隔符设置为csv.reader功能:

csvreader = csv.reader(textfile, delimiter='\t')

您没有在代码中这样做,所以Python不会分割CSV文件的行。这就是它给出错误的原因。在


{MySQL的所有列都可以使用} 命令。它要快得多,然后一行一行地加载。在

^{pr2}$

相关问题 更多 >