python小練習,python練習
python版本使用的是2.7
工作中正好將正在學習的python用上,如果使用java寫的話太麻煩,程式碼數遠比python要多。
第一個例子是根據id的列表產生insert語句插入到某個簡單的表,因為需要做聯集查詢,所以建立一張一個欄位的表,id安行儲存在檔案中:
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 每500行commit一次,涉及到了簡單檔案讀寫操作。
第二個例子是需要提取eclipse產生的.classpath檔案中的classpath路徑,用到了python的Regex:
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
一個典型的.classpath檔案如下:
<?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>
需要提取的是path="" 雙引號中的值。
第三個小例子涉及到了GUI,說是介面其實是python中最簡單的介面編程,使用的是TK,python內建的介面編程庫:
# -*- coding: utf-8 -*- #簡單的日誌排版工具,方便查看日誌資訊,定位問題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 = 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('<ButtonRelease-1>',clearSelectAll)textResult.bind('<Control-Key-c>',clipboard)win.mainloop()
程式中列印的日誌資訊並沒有排版,定位問題查看起來非常不方便,因此做了上面的排版小工具,實現的原理很簡答,就是換行+特定空格列印日誌,原始的日誌資訊類似下面:
20dd5-0dd-dd9 00:47:dd2ddd62|infosmdqwadion|sdsqudssad|q|disdsqwadPdqwymdsnad|OadHdssS|qwGW:dd92ddd68ddd2d223|000000dd0000dd50dddd90047dd2027dd000|||||||
qwonaddsnad= [sdsqObj = DisdsqwadPdqwymdsnadsdsq [qwomdhudqwwdsidjdqwgudqwsdqwgwdpdqwymdsnaddsdsqudssaddDisdsqwadPdqwymdsnadsdsq@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]]
排完後:
20dd5-0dd-dd900:47:dd2ddd62|infosmdqwadion|sdsqudssad|q|disdsqwadPdqwymdsnad|OadHdssS|qwGW:dd92ddd68ddd2d223|000000dd0000dd50dddd90047dd2027dd000|||||||
qwonaddsnad=
[
sdsqObj=DisdsqwadPdqwymdsnadsdsq
[
qwomdhudqwwdsidjdqwgudqwsdqwgwdpdqwymdsnaddsdsqudssaddDisdsqwadPdqwymdsnadsdsq@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
]
]
一直在使用java編程,偶爾用用shell,python既有java強大的功能又有shell的簡單,由於剛學python,雖說它是可以OO的,但是還是習慣當做一種指令碼來寫。