Next, because I think it is too painful to manually copy and paste the 29000 insert statements, I plan to use Python to do this. This is the first time that I write Python code myself. it feels quite smooth. Today, because BeauBeau's lucky draw number was used as an SQL file, it was scared to get the ZIP file unzipped at first-29 CSV files, each file stores 1000 voucher IDs and numbers -_-!
Open each CSV file as before and create a separate SQL file. each SQL statement contains 1000 insert statements, then copy and paste all SQL statements of 29 files into the same SQL file.
The structure of the CSV file is "ID, NUMBER", where ID is a 7-digit NUMBER and NUMBER is an 11-digit NUMBER. In this way, it is more convenient to use the regular expression for capturing. the regular expression used in the search/replace function of Eclipse is "(\ d {7 }), (\ d {11}), the content of the text to be replaced is "insert into cards VALUES ('$ 1',' $ 2', now ());". Use this method to replace the content in 29 CSV files.
All code is as follows:
The code is as follows:
Import sys, OS
Def readFile (filename ):
File = open (filename, "r ")
S = file. read (). strip ()
File. close ()
Return s
Def writeFile (filename, files ):
Content = []
For f in files:
Print "reading file '% s'" % f
S = readFile (f)
Print "read file '% s' completed" % f
Content. append (s)
Print "writing file '% s'" % filename
File = open (filename, "w ")
File. write ("\ n/* ----- This is a seperating line. ----- */\ n". join (content ))
File. close ()
Print "write file '% s' completed" % filename
Filters00000000'.txt ']
Fullpath = OS. getcwd ();
Print "opening directory: '% s'" % fullpath
Sys. path. append (fullpath)
Files = OS. listdir (fullpath)
Files = [f for f in files if OS. path. splitext (f) [1]. lower () in filters]
WriteFile ("beaunet_be_card. SQL", files)
The function of the program is very simple, which is also the first step on the way to Python.
Rewrite this code when you have time, and add the regular expression replacement function.