顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

星期三, 3月 04, 2015

使用 re 模組抓出檔名內的數字


    #() 內的值就是會取得的變動值, \d 代表數字, \d+ 表示多個數字
    import re

    patt = re.compile('NO(\d+)TIME(\d+)')
    mobj = patt.match('NO9TIME1')
    print mobj.groups()  # ('9','2')
    print len(mobj.groups())
    print mobj.group(2)  # 1
   
    # mobj.groups() 會列出 mobj 裡的 group 資料
    # len(mobj.groups()) 就可以知道有多少筆了
   
    # 可使用名稱來讀取抓到的資料 ?P
   
    patt = re.compile('NO(?P\d)TIME(?P\d)')
    mobj = patt.match('NO9TIME2')
    print mobj.group('left_num')  # 9
    print mobj.group('time_num') # 2

    # 如果沒有 match , mobj 為 None

python 讀取目錄內檔案名稱

   
    cur_path = os.getcwd() #得知目前路徑
    for subdir, dirs, files in os.walk(cur_path):
        for file in files:
            # 得到檔案名稱 file

python 寫資料到 xls 檔中

使用 xlwt 模組,但 xlwt 不相容於 python 3.x 
xlwt-future 0.8.0 可以支援 python 3.x

最多支援到 256 column 的資料檔案
用法:
    import xlwt
    from xlwt  import Workbook
   
    wb = xlwt.Workbook(encoding='utf-8')         # open workbook
    ws = wb.add_sheet('First Sheet')                 # add sheet
    ws.write(row_num, col_num,'hello world')    # write data to cell
    wb.save('data_file.xls')                                 # write to xls file


python 3.x print 用法

python 3.x 的 print 無法像 python 2.x 一樣的用法:
3.x: print('hello world')
2.x: print 'hello world'

如果不斷行
3.x: print('hello world', end='')  # 使用空白字元, 或是空字元代替
2.x: print 'hello world',             # 使用 , (逗點)

星期一, 11月 08, 2010

Python 使用 subprocess module 呼叫外部程式

# 當呼叫內建的指令,如 dir 時, Popen()裡的 shell 要設為 True
# stdout 可以設成檔案輸出, 如下:
fopen = open('hello.txt','w')
subprocess.Popen('dir',shell = True, stdout = fopen)
fopen.close()

星期二, 10月 26, 2010

使用 python 呼叫 DLL

使用 BCB 來撰寫 DLL

 extern "C" __declspec(dllexport) int __stdcall ReturnAplusB(int a,int b)  
{
return a+b+1;
}

/* 注意 __stdcall 這個宣告很重要,不然 python 會找不到而出現錯誤訊息 */

使用 python 呼叫 DLL


 from ctypes import *  
def main():
dll = windll.LoadLibrary("DllTest.dll")
c = c_short(1)
d = c_short(3)
a = dll.ReturnAplusB(c,d)
print 'the return value : %d'%a
a = dll.ReturnAplusB(4,6)
print 'the return value : %d'%a
a = dll._Print1()
print 'the return value : %d'%a
if __name__ == '__main__':
main()


## 使用 ctypes module
## 使用 windll, 這個是用來呼叫 windows 中的 DLL.

參考:
http://hi.baidu.com/fangss007/blog/item/461d00f3dc317bc50b46e078.html

星期五, 8月 28, 2009

os.path

使用 os.path.split() 分離路徑及檔名

# === code begin ===
import os
mypath = 'c:\basedir\dirlevel1\dirlevel2\myfile.txt'
basedir, myfilename= os.path.split(mypath)
print basedir, myfilename
# === code end ===

>> c:\basedir\dirlevel1\dirlevel2 myfile.txt

星期四, 8月 27, 2009

在 python 程式中呼叫另一個 python 程式

使用 os.system() 來達成

prog1.py:
# ========= code below ===

print 'this is prog1.py'

# ========= code above ===

prog2.py
# === code below ===

import os

cmd = 'python prog1.py'
os.system(cmd)

# === code above ===

星期三, 8月 05, 2009

How to transfer tuple to list

tuple 無法修改, 要修改其中的 element 就要使用 list. 把 tuple 轉為 list:

a = (1,2,3)
al = list(a)

就這麼簡單...

星期四, 4月 23, 2009

python to exe

. 使用 py2exe (官網 : http://www.py2exe.org/)
. 建立下列程式來轉換 (命名為 setup.py)

from distutils.core import setup
import py2exe

setup(console=['hello.py']) #hello.py 就是要轉換的 .py 檔

.下一步在 cmd line 下執行
d:\> python setup.py py2exe
.轉換完成會產生一個 dist 的目錄, 在該目錄中就會有相對應的 exe 檔及 dll.