python 将csv文件导入到mysql中去

python,2.7  将csv文件导入到mysql中去; 将csv文件放到指定目录里面,最好不要是中文命名

1.csv文件中存有首行字段名

2.没有首行字段名

#!/usr/bin/python
# -*- coding=utf-8 -*-
import xlrd
import string
import os
import pymysql
import time
import datetime
import sys
reload(sys)
sys.setdefaultencoding("utf8")




def IsSubString(SubStrList,Str):
    '''''
    #判断字符串Str是否包含序列SubStrList中的每一个子字符串
    #>>>SubStrList=['F','EMS','txt']
    #>>>Str='F06925EMS91.txt'
    #>>>IsSubString(SubStrList,Str)#return True (or False)
    '''
    flag=True
    for substr in SubStrList:
        if not(substr in Str):
            flag=False
    return flag



def fn_get_filelist(FindPath,FlagStr=[]):
    '''''
    #获取目录中指定的文件名
    #>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符
    #>>>FileList=GetFileList(FindPath,FlagStr) #
    '''
    FileList=[]
    FileNames=os.listdir(FindPath)
    if (len(FileNames) > 0):
       for fn in FileNames:
           if (len(FlagStr) > 0):
               #返回指定类型的文件名
               if (IsSubString(FlagStr,fn)):
                   fullfilename=os.path.join(FindPath,fn)
                   FileList.append(fullfilename)
           else:
               #默认直接返回所有文件名
               fullfilename=os.path.join(FindPath,fn)
               FileList.append(fullfilename)
    #对文件名排序
    if (len(FileList)>0):
        FileList.sort()
    for i in range(len(FileList)):
        print FileList[i]
    print FileList
    return FileList


def fn_exists_drop(f):

    t_prefix = 'tmp_excel' + time.strftime("%Y%m%d",time.localtime()) + '_'
    tt = 'drop table if exists `' + t_prefix + unicode(f.split("\\")[-1].split(".")[0],"utf-8") + '`; '


    return tt


def fn_getFirsRow(f,isHaveTitle,splitBy):
    ff = open(f)
    ffline=0
    for fline in ff :
        ffline = ffline + 1
        fl = fline
        break
    return fl

        #print cols,len(cols)
    #pass


def fn_insert_by_txt(f,  isHaveTitle):
    print 'f'*100
    tablename = f.split('\\')[1]
    print 'tablename=' ,tablename

    mydb = pymysql.connect("127.0.0.1","root","Root123.","jintai2",port=3306,charset='utf8' )
    mycursor = mydb.cursor()
    mycursor.execute('SET CHARACTER SET utf8;')
    mycursor.execute('SET NAMES utf8;')
    mycursor.execute('SET character_set_connection=utf8;')
    ff = open(f)
    print '.'*100
    lineNum = 0
    for fline in ff :
        lineNum += 1
        if lineNum==1 and isHaveTitle==1 :
            # tablename.decode('utf8') + */ unicode(utf8string, "utf-8") unicode(utf8string, "utf-8")
            print '-'*10
            flines = fline.decode('gb2312')
            print type(fline)    #+ fline.replace('"','`').replace(',',' varchar(255) ,')
            tExists, tExists_sql = 0, "SELECT COUNT(*) FROM `information_schema`.`TABLES` AS a WHERE a.`TABLE_SCHEMA` = DATABASE() AND table_name ='"+tablename+"'"
            mycursor.execute(tExists_sql)
            rExists = mycursor.fetchall()
            for rr in rExists:
                tExists = rr[0]
                print 'tExists',tExists
            if tExists==1:
                cols_sql = " SELECT GROUP_CONCAT( CONCAT('`',a.`COLUMN_NAME`,'`')  ORDER BY `ORDINAL_POSITION`) FROM `information_schema`.`COLUMNS` AS a WHERE a.`TABLE_SCHEMA` = DATABASE()  AND COLUMN_NAME<>'pkid' AND table_name ='"+tablename+"'"
                mycursor.execute(cols_sql)
                cExists = mycursor.fetchall()
                for cc in cExists:
                    cols = cc[0]
                    print type(cols),cols
                    #sys.exit()

            else:
                sqlCreate =  ' create table if not exists `' + tablename +'`( '  + flines.replace('"','`').replace(',',' varchar(255) ,') +   '  varchar(225) , pkid int AUTO_INCREMENT primary key )'
                cols = flines.replace('"','`')
                print sqlCreate
                mycursor.execute(sqlCreate)
                mydb.commit()
        else:
            try:
                insertSql = ' insert into `' + tablename + '`  (' + cols + ' ) values ( '
                flines = fline.decode('gb2312')
                v = (  flines.replace(chr(39),'”').replace('\n',''))
                #print type(v)
                insertSql =  insertSql  + v + ')'
                #print insertSql
                mycursor.execute(insertSql)
                print tablename ,lineNum
                if lineNum/100==0   :
                    mydb.commit()

            except Exception , e:
                print e
    mydb.commit()
    ff.close()


def fn_arr_maxLen(arr):
    o = 0
    for v in arr:
        if (len(str(v).strip()) > o):
            o = len(str(v).strip())
    return o

if __name__ == '__main__':

    fs=fn_get_filelist(u"F:/dm_excel",['csv'])
    print fs
    for f in fs:
        fn_insert_by_txt(f,   1)


 

 

你可能感兴趣的:(python 将csv文件导入到mysql中去)