Python and python

Source: Internet
Author: User

Python and python

Python 2.7 is used.

It is better to use the python that you are learning at work. It is too troublesome to write in java, and the number of lines of code is far greater than that of python.

The first example is to generate an insert statement based on the list of IDs and insert it into a simple table. Because joint queries are required, create a new table with a field. The id is stored in the file:

import oscurdir = os.getcwd()count = 0sql = 'delete from test_userid;\ncommit;\n'destFile = os.path.join(curdir,'mobile.sql')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        if count % 500 == 0:            sql+='commit;\n'with open(destFile,'w') as fw:    fw.writelines(sql)    fw.close()print 'convert success and the count is : %d' %count         
Each 500 lines of commit occurs, involving simple file read/write operations.

The second example is to extract the classpath path in the. classpath file generated by eclipse and use the python regular expression:

import rewith open("D:\workspace\Client\.classpath") as f:    cpStr = ''    for line in f.readlines():        p = re.compile(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 in path = "" Double quotation marks must be extracted.

The third small example involves the GUI, which means that the interface is actually the simplest Interface Programming in python, using TK and the interface programming library that comes with python:

#-*-Coding: UTF-8-*-# simple log formatting tool to conveniently view log information and locate the problem from Tkinter import * # create the main window win = Tk () # create input Text box textArea = Text (win) textArea. pack () # create a frameframe = Frame (win) frame. pack () # specify the parent container of the button as framebutton = Button (frame, text = 'fomat ') button. pack (side = LEFT) clearButton = Button (frame, text = 'clear') clearButton. pack (side = RIGHT) # create result Text box textResult = Text (win) textResult. pack (side = LEFT) # create a scroll bar s = Scrollbar (win) s. pack (side = RIGHT, fill = Y) # bind textResult and the scroll bar textResult ['yscrollcommand'] = s. sets ['command'] = textResult. yviewdef formatText (e): count = 0 flag = 0 # obtain text content = textArea. 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 elif text = ']' or text = '}': count-= 1 printBalnk (count, text, 0) else: printCommon (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 = ',': textResult. 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_append (content) def clearText (e): textArea. delete (0.0, END) textResult. delete (0.0, END) button. bind ('<Button-1>', formatText) clearButton. bind ('<Button-1>', clearText) textResult. bind ('<Control-Key-a>', selectText) textResult. bind (', clearSelectAll) textResult. bind ('<Control-Key-c>', clipboard) win. mainloop ()

The log information printed in the program is not typeset, and it is very inconvenient to locate the problem. Therefore, the preceding typographical tool is used to print logs with line breaks and specific spaces, the original log information is similar to the following:

20dd5-0dd-dd9 00: 47: dd2ddd62 | infosmdqwadion | sdsqudssad | q | average | OadHdssS | qwGW: dd92ddd68ddd2d223 | average |
Qwonaddssad = [sdsqObj = Shanghai [Shanghai @ 7224f60qw, Shanghai = Shanghai [Shanghai @ 5895qw85f, Beijing = null, qwussdsnqwy = 0, dsxaddsnsionInfo = [Shanghai [kdsy = Beijing, vdqwluds = 0], kernel [kdsy = qwlidsnadID, vdqwluds = null], kernel [kdsy = isGudqwsdqwnaddsds, vdqwluds = 0]

After sorting:

20dd5-0dd-dd900: 47: dd2ddd62 | infosmdqwadion | sdsqudssad | q | disdsqwadpdqwymdsnad| OadHdssS | qwGW: dd92ddd68ddd223 | medium |
Qwonaddssad =
[
SdsqObj = DisdsqwadPdqwymdsnadsdsq
[
Alias @ 7224f60qw,
QwsdsdqwaddsPdqwymdsnadInfo = qwsdsdqwaddsPdqwymdsnadInfo
[
QwomdhudqwwdsidjdqwgudqwsdqwgwdsqwhdsmdqwdqwsdsdqwaddsPdqwymdsnadInfo @ 5895qw85f,
QwdqwnqwdslUsL = null,
Qwussdsnqwy = 0,
DsxaddsnsionInfo =
[
NdqwmdsdPdqwsdqwmdsaddss
[
Kdsy = isGudqwsdqwnaddsds,
Vdqwluds = 0
],
NdqwmdsdPdqwsdqwmdsaddss
[
Kdsy = qwlidsnadID,
Vdqwluds = null
],
NdqwmdsdPdqwsdqwmdsaddss
[
Kdsy = isGudqwsdqwnaddsds,
Vdqwluds = 0
]
]

I have been using java programming and occasionally using shell. python has both powerful java functions and shell simplicity. Since I just learned python, although it can be OO, but I am still used to writing as a script.

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.