Copy and paste Python program

Source: Internet
Author: User
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.

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: 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.