Python Little Exercise

Source: Internet
Author: User

The Python version uses 2.7

The job is to use the python you're learning, and it's too cumbersome to write in Java, with more lines of code than Python.

The first example is the INSERT statement that is generated from the list of IDs inserted into a simple table, because a federated query is required, so create a new table with a single field, and the ID line is stored in the file:

Import Oscurdir = OS.GETCWD () count = 0sql = ' Delete from test_userid;\ncommit;\n ' destfile = Os.path.join (CurDir, ' mobile.s QL ') with open (Os.path.join (CurDir, ' Mobile.dat ')) as F: for    UserID in F.readlines ():        if UserID = = ' \ n ':            Continue        userid = Userid.strip (' \ n ')        sql + = ' INSERT into Test_userid values (\ ' + userid + ' \ '); \ n '        count+=1< C6/>if count% = = 0:            sql+= ' commit;\n ' with open (DestFile, ' W ') as FW:    fw.writelines (SQL)    fw.close () print ' Convert success and the count is:%d '%count         
Every 500 lines commit once, involving simple file read and write operations.

The second example is the need to extract the classpath path in the. classpath file generated by eclipse, using the python regular expression:

Import Rewith Open ("D:\workspace\Client\.classpath") as f:    cpstr = "for line in    F.readlines ():        p = Re.compi Le (R '. *kind= "Lib". *path= "(. *?)")        result = P.findall (line)        if Len (result) > 0:            cpstr + = P.findall (line) [0]            cpstr + = ";"    Print Cpstr

A typical. classpath file is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><classpath><classpathentry kind= "src" path= "Src/main/java"/ ><classpathentry kind= "src" path= "src/main/resources"/><classpathentry kind= "con" path= " Org.eclipse.jdt.launching.JRE_CONTAINER "/><classpathentry kind=" Lib "path=" lib/thirdparty/dsf/ Protostuff-runtime-1.0.7.fixed.3300.jar "/><classpathentry kind=" Lib "path=" Lib/thirdparty/dsf/xz-1.4.jar "/ ><classpathentry kind= "Lib" path= "Lib/thirdparty/dsf/zookeeper-3.4.5.jar"/><classpathentry kind= "Lib" Path= "Lib/commonservice.api-v300r003c20b301-snapshot.jar"/><classpathentry kind= "Output" path= "classes"/ ></classpath>

The value that needs to be extracted is path= "" in double quotation marks.

The third small example relates to the GUI, which is actually the simplest interface programming in Python, using Tk,python's own interface programming library:

#-*-Coding:utf-8-*-#简单的日志排版工具, convenient to view log information, locate the problem from Tkinter import * #创建主窗口win = Tk () #创建输入文本框textArea = Text (Win) TextArea. Pack () #创建frameframe = Frame (Win) frame.pack () #指定button的父容器为framebutton = Button (frame,text= ' Fomat ') button.pack (side =left) Clearbutton = Button (frame,text= ' Clear ') clearbutton.pack (side=right) #创建结果文本框textResult = text (Win) Textresult.pack (side=left) #创建滚动条s = Scrollbar (Win) s.pack (side = Right,fill = Y) #绑定textResult和滚动条textResult [' Yscrollcommand '] = s.sets[' command '] = textresult.yviewdef formattext (e): Count = 0 flag = 0 #获取文本 content = t        Extarea.get (0.0,end) for I in range (len (content)): Text = content[i] if text = = ': Continue if text = = ' [' or text = = ' {': flag = 1 Printbalnk (count, text) count+=1 El if text = = '] ' or text = = '} ': Count-=1 Printbalnk (count, text,0) Else:printcommo N (count,text,flag) if text = = ', ': FLAG = 1 Else:flag = 0def Printbalnk (count,text,flag=1): Textresult.insert (current, ' \ n ') for I in range (count): Textresult.insert (Current, ') if flag = = 1:textresult.insert (current,text + ' \ n ') ) Else:textResult.insert (Current,text) def Printcommon (Count,text,flag): if flag = = 1:for I in range ( count): Textresult.insert (Current, ') Textresult.insert (current,text) if text = = ', ': textres Ult.insert (current, ' \ n ') def SelectText (e): Textresult.tag_add (' Select_all ', ' 1.0 ', END) textresult.tag_config (' Select_all ', foreground= ' red ') return ' break ' Def Clearselectall (e): Textresult.tag_config (' Select_all ', foreground= ' Black ') def clipboard (e): content = Textresult.get (' 1.0 ', END) textresult.clipboard_clear () Textresult.clipboard_ap  Pend (content) def cleartext (e): Textarea.delete (0.0, end) textresult.delete (0.0, end) button.bind (' <Button-1> ', Formattext) clearbutton.bind (' &LT Button-1> ', cleartext) textresult.bind (' <Control-Key-a> ', SelectText) textresult.bind (' < Buttonrelease-1> ', Clearselectall) textresult.bind (' <Control-Key-c> ', clipboard) Win.mainloop ()

Program in the printing of the log information is not typesetting, positioning problems to view is very inconvenient, so did the above typesetting gadget, the implementation of the principle is very simple answer, is a newline + specific space print log, the original log information similar to the following:

20DD5-0DD-DD9 00:47:dd2ddd62|infosmdqwadion|sdsqudssad|q|disdsqwadpdqwymdsnad| oadhdsss|qwgw:dd92ddd68ddd2d223|000000dd0000dd50dddd90047dd2027dd000| | | | | | |
qwonaddsnad= [sdsqobj = DISDSQWADPDQWYMDSNADSDSQ [Qwomdhudqwwdsid[email protected]7224f60qw, Qwsdsdqwaddspdqwymdsnadinfo=qwsdsdqwaddspdqwymdsnadinfo [Qwomdh[email protected]5895qw85f, QwdqwnqwdslUsL=null, Qwussdsnqwy=0, Dsxaddsnsioninfo=[ndqwmdsdpdqwsdqwmdsaddss [Kdsy=isgudqwsdqwnaddsds, Vdqwluds=0], Ndqwmdsdpdqwsdqwmdsaddss [Kdsy=qwlidsnadid, Vdqwluds=null], Ndqwmdsdpdqwsdqwmdsaddss [Kdsy=isGudqwsdqwnaddsds, Vdqwluds=0]]

After you finish the line:

20dd5-0dd-dd900:47:dd2ddd62|infosmdqwadion|sdsqudssad|q|disdsqwadpdqwymdsnad| oadhdsss|qwgw:dd92ddd68ddd2d223|000000dd0000dd50dddd90047dd2027dd000| | | | | | |
Qwonaddsnad=
[
Sdsqobj=disdsqwadpdqwymdsnadsdsq
[
Qwomdhudqwwdsid[email PROTECTED]7224F60QW,
Qwsdsdqwaddspdqwymdsnadinfo=qwsdsdqwaddspdqwymdsnadinfo
[
Qwomdh[email protected]5895qw85f,
Qwdqwnqwdslusl=null,
Qwussdsnqwy=0,
dsxaddsnsioninfo=
[
Ndqwmdsdpdqwsdqwmdsaddss
[
Kdsy=isgudqwsdqwnaddsds,
Vdqwluds=0
],
Ndqwmdsdpdqwsdqwmdsaddss
[
Kdsy=qwlidsnadid,
Vdqwluds=null
],
Ndqwmdsdpdqwsdqwmdsaddss
[
Kdsy=isgudqwsdqwnaddsds,
Vdqwluds=0
]
]

has been using Java programming, occasionally with Shell,python both Java powerful features and shell simple, because just learn python, although it can oo, but it is customary as a script to write.

Python Little Exercise

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.