Python program for copy-and-paste functionality

Source: Internet
Author: User
Tags readfile
Today because of the lottery numbers provided to Beaubeau to do SQL files, initially received ZIP file decompression compression was frightened--29 CSV files, each file saved 1000 lottery ID and number-_-!

As before, open each CSV file to do a separate SQL file, with 1000 inserts in each SQL, and then copy and paste all the SQL statements from the 29 files into 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. It is convenient to use regular-style capture, and the regular formula used in Eclipse's Find/Replace function 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 the 29 CSV files.



All codes are 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


filters=['. 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 simple, and this is my first step on the path of Python.

Rewrite this code when you have time, and join the regular replacement function
  • 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.