• 沒有找到結果。

Python 10

N/A
N/A
Protected

Academic year: 2021

Share "Python 10"

Copied!
26
0
0

加載中.... (立即查看全文)

全文

(1)
(2)

Outline

 內建標準函式庫

 第三方函式庫

(3)

模組的使用

 建立模組  建立一個A.py檔  再建立第二個B.py檔  載入模組  在第二個檔案輸入import A

 亦可使用from ModuleName import FunctionName  Python會將載入的模組編譯成A.pyc於B.py檔旁邊

 使用第二個方法可以使用'*'表示載入所有函數

 用help()函數可以將模組定義的函數備註文字輸出

(4)

使用標準函數庫

 使用os模組  提供顯示系統環境參數與指令功能函數  os.rename(src, dst)  對檔案或目錄更換名稱  src引數是原本的資料夾  dst引數是修改後的資料夾名稱  os.remove(path)  移除檔案  path引數傳入檔案位置  不會移除資料夾  os.removedirs(path)  移除空的資料夾  os.listdir(path)  輸出path引數位置的目錄和檔案名稱 4

(5)

使用標準函數庫

 使用os模組  os.chdir(path)與os.getcwd()  os.chdir(path)函數是切換目錄到path引數位置  os.getcwd()是顯示目前所在的目錄位置  os.mkdir(path[, mode])與os.rmdir(path)  os.mkdir()是建立資料夾  path引數是建立/刪除目錄的位置  mode引數是Unix平台使用的  os.rmdir()函數是刪除目錄  os.path.getsize(path)  取得檔案大小  os.path.getctime(path)  取得檔案的建立日期 5

(6)

使用標準函數庫

 使用os模組  os.path.getmtime(path)  取得檔案的修改日期  os.path.getatime(pah)  取得檔案的存取日期  os.path.isfile(path)  判斷傳入的path引數是否為檔案  os.path.isdir(path)  判斷傳入的path引數是否為目錄  http://docs.python.org/3.3/library/os.path.html

(7)

使用標準函數庫

 使用random模組  random.random()  隨機產生0.0<=x<1.0之間的數字  random.uniform(num1, num2)  隨機產生num1<=x<num2的數字  random.randint(1,10)  產生一個1到10的數字  random.randrange(0,101)  產生一個0到100的數字 7

(8)

使用標準函數庫

 使用random模組  random.choice()  將傳入的內容隨機取得  random.suffle()  將傳入的序列型態資料內的項目順序以隨機順序產生  random.sample()  將傳入的序列項目以指定的長度顯示一個隨機項目值的序列 

http://docs.python.org/3.3/library/random.html?h

ighlight=random#random

8

(9)

使用標準函數庫

 使用time模組  time.time()  取得系統時間  time.sleep(num)  設定暫停時間  time.localtime()  回傳的格式如下:

 time.struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday_, tm_isdst)

time.gmtime()

 取得UTC時間

(10)

使用標準函數庫

 使用sys模組  sys.argv[0]  會回傳此程式檔案的位置與名稱  sys.argv>1  是判斷是否有帶入參數  sys.builtin_module_names  回傳Python程式語言內所有內置模組名稱  sys.modules.keys()  得知目前已經載入的模組  sys.platform  取得目前作業系統的版本  sys.exit()  宣告sys.exit(0)終止程式

(11)

使用標準函數庫

 使用sys模組  sys.version

 回傳目前安裝在系統上的Python版本

 格式:’(#build_number, build_date, build_time) [compiler]’  sys.api_version  回傳Python直譯器的C API版本  sys.version_info  回傳一個tuple型態的值  (‘主要版本’, ’次要版本’, ’小版本’)  sys.winver  回傳的版本數字是註冊在Windows裡的Python版本  sys.path  定義Python搜尋模組的路徑 11

(12)

使用標準函數庫

 建立tar.gz壓縮檔  宣告使用tarfile.open(path, mode)建立壓縮檔  mode引數為'a'代表要自動建立壓縮檔並將檔案添加進 去  使用tar.add(name)函數將檔案加入壓縮檔內  用tar.close()將壓縮檔關閉  讀取tar.gz壓縮檔  tar.getmembers() Ex:

for file in tar.getmembers():

print("%s\t\t\t%s" % (file.name, file.size))

(13)

使用標準函數庫

 使用shutil模組  比較高階的應用層,提供數個針對檔案操作的功能  shutil.copytree(src, dst)  複製整個目錄,包含目錄內的所有檔案  shutil.copy(src, dst)  複製檔案  shutil.rmtree(path)  移除整個目錄,包含目錄內的所有檔案  shutil.move(src, dst)  移動檔案,移動時也可以進行更換檔案名稱  shutil.copystat(src, dst)  複製檔案,會連同檔案屬性一同複製 13

(14)

練習

 建立一個檔案random.txt 檔案內容為  生成 100個1~1000的奇數存入檔案 (一個數字一行,且數 字不重複)  使用os模組讀取此檔案  使用time模組將此檔案的印出建立日期、修改日期  建立一個名為test的目錄  使用

shutil模組將random.txt移動至test目錄下

(15)

第三方函式庫

 python社群提供了大量的第三方模組,使用方式與標 準庫類似。它們的功能無所不包,覆蓋科學計算、 Web開發、資料庫介面、圖形系統多個領域,並且大 多成熟而穩定。第三方模組可以使用Python或者C語 言編寫。  您也許聽過「不要重造輪子」這句話,或是 DRY

(Don't Repeat Yourself),講得就是「別人已經寫好的 東西,就拿去用吧,不用自己再重新寫一套」。

(16)

安裝第三方函式庫

 方法1

 下載原始碼,手動執行 python setup.py install安装

 方法2:利用第三方安裝工具(如pip,easy_install,

(17)

Python3 Packages

 列出所有支援python3以上的函式庫

 https://pypi.python.org/pypi?:action=browse&c=533& show=all

(18)

pip 1.3.1

A tool for installing and managing Python packages.

(19)

pip安裝步驟

 1.下載原始檔

https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz

 2.解壓縮

 3.執行 python setup.py install

(20)

設定環境變數

(21)

distribute 0.6.45

Easily download, build, install, upgrade, and uninstall Python packages

(22)

PyQRCode 0.09

 QR Code生成器,使用python3寫成的可以輸出SVG與

PNG的格式

(23)

PyQRCode 範例

 將網址輸出成svg格式,比例設定為8

from pyqrcode import QRCode

url = QRCode('http://www.ntu.edu.tw')

#url.svg('url.svg', scale=8)

(24)

PyQRCode 範例

 將網址輸出成png格式,比例設定為10

from pyqrcode import QRCode

url = QRCode('http://www.ntu.edu.tw')

url.png(‘qrcode.png',scale=10)

(25)

安裝練習

 試著安裝 pypng套件

 https://pypi.python.org/pypi/pypng/0.0.15

(26)

練習

 試著讀取 urls.txt 檔案中的超連結

 並將其輸出成Qrcode存成png檔

參考文獻

相關文件

• You need to know programming (C, python, java, … anything)
 (but not necessarily Matlab).. • You need to know how to find

Laser Microdissection Microdissection for pure DNA, RNA, for pure DNA, RNA, Proteins and Living Cells Proteins and Living

課程利用雲端學習平台 OpenEdu 從最基礎開始說明 Python 的語 法與應用,配合 Quiz in Video

The tree’s roots can easily find water in the ground and send it up to the leaves.. Because they are wide and thin, leaves lose

Micro:Bit AI2 Scratch Mbot Python

XAMPP is a completely free, easy to install Apache distribution.. containing MariaDB, PHP,

experiment may be said to exist only in order to give the facts a chance of disproving the

Binomial Option Pricing