#coding: Utf-8 ' @2016/04/13:to load raw-data.csv into db. Data is from CiteULike. " Import reimport mysql.connectordef Main (): # directory on Iiot ' s server infile = Open ('./raw-data.csv ', ' R ') Outfi Le = Open ('./error.txt ', ' A + ') #database on Iiot ' s server CNX = Mysql.connector.connect (user= ", password=", Host = ', Port =, db= ') cursor = cnx.cursor () i Nsert_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.grou P (3), Res.group (4), Res.group (5)) Cursor.execute (Insert_query,data) cnx.commit () except Excepti On,e:outfile.write (str (e) + ' \ n ' +read+ ' \ n ') If __name__== ' __main__ ': Main ()
Import the. csv file into the Mysql,.csv file with ', ' delimited, in each string containing ', ' cannot be used with the load data local infile. Fields terminated by ', ' otherwise the string is truncated.
with RE
Import MySQL with Python