python将CSV导入mysql

时间:2018-04-20 22:25:08

标签: python mysql linux csv import

我有这个CSV文件

W011515822935S27462G9150,7876,37228,20088,20084,5901,5900,65392,7867,16731,2743,65388,49233,6286,7467,65384,6281,65376,65374,2726,2724,2723,2722,2721 ,3800,65363,65362,65361,65360,4880,65358,65357,65354,3690,3491,3687,3686,5967,5332,6132,33223,8392,65359,7402,47286,3847,3845,3841,60181 ,24128,67712,10143,6197,5604,5603,5008,5327,5328,7563,3818,65367,3808,119051,3805,3998,24091,65390,6130,5608,38649,15661,6134,6133,38637 ,6131,38635,7507,38631,47284,6124,6123,6122,6121,6120,5331,6118,5329,65413,5918,47288,80776,17941,65407,37235,3493

W011515883341S27461G8194,7876,37228,20084,5901,5900,65392,7867,16731,2743,65388,49233,6286,7467,65384,65382,6281,65376,65374,2726,2724,2723,2722,2721 ,3800,65363,65362,65361,65360,4880,65358,65357,65354,3690,3491,3687,3686,5967,5332,6132,33223,8392,65359,7402,47286,3847,3845,3841,60181 ,24128,67712,10143,6197,5604,5603,5008,5327,5328,7563,3818,65367,3808,119051,3805,3998,24091,35710,65390,6130,5608,38649,15661,6134,6133 ,38637,6131,38635,7507,38631,47284,6124,6123,6122,6121,6120,5331,5330,5329,65413,5918,47288,80776,17941,65407,37235

其中第一个值是第一列(W011515883341S27461G8194),其余的是第二列(7876,37228,20084,5901 ..............)

我需要将这些数据导入mysql 我做了这个剧本

`# -*- coding: utf-8 -*-

import csv
import MySQLdb
import os
import gzip
import time
import datetime
from datetime import datetime
from dateutil.relativedelta import relativedelta

 #DATE
 date_after_day = datetime.today()+ relativedelta(days=-1)
 date  = date_after_day.strftime('%Y%m%d')
 print(date)

#CONVERTR FILE FROM TSV TO CSV
os.system('gunzip /mnt/new_segments/lotame_extra/'+ date 
+'16/audiencemembership.tsv.gz')
os.system('sed "s/\t/,/" /mnt/new_segments/lotame_extra/'+ date 
+'16/audiencemembership.tsv > /mnt/new_segments/lotame_extra/'+ date 
+'16/audiencemembership.csv')


#INSERT CSV INTO DATABASE
mydb = MySQLdb.connect(host='10.2xx.xx.xx', user='xxx', passwd='xxx', 
db='lotxx')
cursor = mydb.cursor()
csv_data = csv.reader(file('/mnt/new_segments/lotxx_extra/'+ date 
+'16/audiencemembership.csv'))

 for row in csv_data:
    print row
    cursor.execute("INSERT INTO audiencemembership_temp (id_audience, 
 audience) VALUES (%s, %s)", tuple(row))
    print row
 mydb.commit()
 cursor.close()
 print "Done"'

我收到了这个错误

[' W011513245766S26286G-952',' 3491',' 7563',' 17941',' 37235' ,' 5901', ' 5608',' 65358',' 5604',' 5603',' 6281',' 5332& #39;,' 6121',' 3845',' 3841', ' 10143',' 49233',' 5967',' 15661',' 7402',' 7507& #39;,' 119051',' 4880', ' 2743',' 3805',' 3800',' 2726',' 6120',' 2724& #39;,' 2723',' 2722']

追踪(最近一次通话): 文件" test2.py",第30行,in cursor.execute(" INSERT INTO audiencemembership_temp(id_audience,audience)VALUES(%s,%s)",tuple(row)) File" /usr/lib64/python2.7/site-packages/MySQLdb/cursors.py" ;,第187行, 执行中 query = query%tuple([db.literal(item)for args中的item]) TypeError:不是在字符串格式化期间转换的所有参数

0 个答案:

没有答案