python不能导入mysql,用python导入mysql

#coding: utf-8

‘‘‘

@2016/04/13:

To load raw-data.csv into db.

Data is from Citeulike.

‘‘‘

import re

import mysql.connector

def main():

# directory on iiot‘s server

infile = open(‘./raw-data.csv‘,‘r‘)

outfile = open(‘./error.txt‘,‘a+‘)

#database on iiot‘s server

cnx = mysql.connector.connect(user=‘ ‘,password=‘ ‘,

host = ‘ ‘,port = ,

db=‘ ‘)

cursor = cnx.cursor()

insert_query = ‘insert into article values (%s,%s,%s,%s,%s)‘

for read in infile:

# 1 2 3 4 5

# doc.id,"title","citeulike.id","raw.title","raw.abstract"

try:

res = re.search(‘(\d*),"(.*)",(\d*),"(.*)","(.*)"‘,read)

print res.group(1)

data = (res.group(1),res.group(2),res.group(3),res.group(4),res.group(5))

cursor.execute(insert_query,data)

cnx.commit()

except Exception,e:

outfile.write(str(e)+‘\n‘+read+‘\n‘)

if __name__==‘__main__‘:

main()

把.csv文件导入mysql,.csv文件以‘,‘分隔,在每一项字符串中含有‘,‘ 不能用load data local infile .. fields terminated by ‘,‘,否则字符串被截断。

用re

原文:http://www.cnblogs.com/yuchenkit/p/5388117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值