开发者

I have a tab delimeted file that I want to convert into a mysql table

开发者 https://www.devze.com 2022-12-27 05:32 出处:网络
I have a tab delimeted file that I want to convert into a mysql table. there are 25 tab delimeted fields in the text file.

I have a tab delimeted file that I want to convert into a mysql table. there are 25 tab delimeted fields in the text file. I can get the values in when I construct the SQL statement word by word and get each value individually stated in the VALUES part but when I try to get the list as a whole it do开发者_高级运维es not work. Here is the code. I couldn't figure it out. Any ideas?

lines=open(path, "r").readlines()

for line in lines[1:]:

 linex=line.strip().split("\t")
 linex.insert(0,'sometextindex')       

 try:
  cursor.execute('INSERT INTO variants VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',linex) 
 except:
  print 'line number=',a,linex


Why not just use LOAD DATA INFILE from mysql?


stmt="%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % (linex[0],linex[1],linex[2], ........ )
....
cursor.execute(stmt) 
....
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号