Today because the lottery number provided to Beaubeau to do the SQL file, a first received zip file to untie the compression was scared--29 CSV files, each file saved 1000 lottery IDs and numbers-_-!
As last time, open each CSV file to do a separate SQL file, each SQL has 1000 INSERT statements, and then copy all the SQL statements of 29 files to the same total SQL file.
The structure in the CSV file is the structure of "Id,number", where the ID is 7 digits and number is 11 digits. This is a convenient way to capture with a regular formula, the regular formula used in Eclipse's Find/Replace feature is "(\d{7})," (\d{11}), and the textual content of the replacement is "INSERT into cards VALUES (' $ ', ' $ '), Now ()); ". Use this method to replace the contents of a 29 CSV file.
All the code is as follows:
Copy Code code 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 are a seperating line.-----*/\n". Join (content))
File.close ()
Print "Write file '%s ' completed"% filename
filters=[' txt ']
FULLPATH=OS.GETCWD ();
Print "Opening directory: '%s '"% FullPath
Sys.path.append (FullPath)
Files = Os.listdir (FullPath)
Files =[f for F into files if Os.path.splitext (f) [1].lower () in filters]
WriteFile ("Beaunet_be_card.sql", files)
The function of the program is simple, and this is my first step on the Python road.
Rewrite this code when you have time to add the regular replacement function