Copy and paste the Python program _python

Source: Internet
Author: User
Tags readfile
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.