zoukankan      html  css  js  c++  java
  • 用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)+'
    '+read+'
    ')
            
    if __name__=='__main__':
        main()
    

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

    用re

  • 相关阅读:
    java笔记
    java面向对象
    Oracle数据库基础
    Java中的集合和常用类
    Java面向对象的三个特征
    Java中的类与对象
    Java中的冒泡排序
    JAVA中的一些内置方法
    JAVA中的数据类型
    SSH整合
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/5388117.html
Copyright © 2011-2022 走看看