解決missing config-win.h & mysqlclient.lib.的問題
|
2016年2月23日 星期二
Python mysql library安裝
2016年2月15日 星期一
Python standard library string
python standard libray string
提供了一些字串操作基本功能
提供了一些字串操作基本功能
str.translate(table[, deletechars])
translate可以依據table來轉換str
如果轉換的目標是None,則match的字元就會被刪除
>>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt'
參考網址https://docs.python.org/2.7/library/string.html?highlight=translate#module-string
python standard library os
python standard library裡提供os這個module
可以進行os的一些基本操作
例如取得資料夾底下的檔案名
os.listdir(path)
列出路徑下,所有檔案名字
os.rename(src,dst)
將src檔名改成dst
os.getcwd( )
取得work directory 路徑
os.chdir(path)
改變work directory 路徑
參考網址
https://docs.python.org/2/library/os.html
可以進行os的一些基本操作
例如取得資料夾底下的檔案名
os.listdir(path)
列出路徑下,所有檔案名字
os.rename(src,dst)
將src檔名改成dst
os.getcwd( )
取得work directory 路徑
os.chdir(path)
改變work directory 路徑
參考網址
https://docs.python.org/2/library/os.html
2016年2月11日 星期四
python escape convert
要將字串轉成escape的型式
程式碼如下
def escape_covert(s):
for (i, o) in ( ('&', '&'),
('>', '>'),
('<', '<'),
('"', '"'),
):
s = s.replace(i, o)
return s
要注意 ('&', '&')要放第一個
否則其他escape charater
轉換完的&會被轉成'&
造成錯誤
但有更簡便的做法
就是import python的standard library cgi
import cgi
def escape_covert(s):
return cgi.escape(s,quote=True)
參考網址
https://docs.python.org/2/library/cgi.html
程式碼如下
def escape_covert(s):
for (i, o) in ( ('&', '&'),
('>', '>'),
('<', '<'),
('"', '"'),
):
s = s.replace(i, o)
return s
要注意 ('&', '&')要放第一個
否則其他escape charater
轉換完的&會被轉成'&
造成錯誤
但有更簡便的做法
就是import python的standard library cgi
import cgi
def escape_covert(s):
return cgi.escape(s,quote=True)
參考網址
https://docs.python.org/2/library/cgi.html
Python string formatting
Python string formatting有二種
% 和 format method
%是比較舊的方式
建議使用format,比較恰當
在讀程式時較易理解
% 和 format method
%是比較舊的方式
建議使用format,比較恰當
在讀程式時較易理解
s1 = "John!"
s2 = "love"
print "i am %s" % s1 #i am John
print "i am {0}".format(s1) #i am John
print "with %(kwarg)s!" % {'kwarg':sub2} #with love
print "with {kwarg}!".format(kwarg=sub2) #with love
2016年2月8日 星期一
Python Standard Library time
Python Standard Library裡有time module提供相關的功能
例如
想要暫停現在的動作可以使用sleep
time.sleep(1)
取得現在的時間可以用ctime
time.ctime( )
會回傳 星期 月份 日期 時:分:秒 年的型式,例如Mon Feb 08 23:24:26 2016
參考網站
https://docs.python.org/2/library/time.html
例如
想要暫停現在的動作可以使用sleep
time.sleep(1)
程式就會暫停1秒,再執行後面的動作
取得現在的時間可以用ctime
time.ctime( )
會回傳 星期 月份 日期 時:分:秒 年的型式,例如Mon Feb 08 23:24:26 2016
參考網站
https://docs.python.org/2/library/time.html
Python Standard Library webbrowser
Python Standard Library裡有webbrowser這個module
webbrowser提供簡單的function,來執行瀏覽器
例如
就會打開瀏覽器,瀏覽輸入url的網址的網頁
如果要指定特定瀏覽器來瀏覽
可以使用webbrowser.get來得到object
再使用這個object來呼叫open function
firefox = webbrowser.get('C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s')
firefox.open("www.google.com")
這裡要注意的是傳入get function裡的路徑
與windows裡路徑使用' \ ' 不同
要使用unix-style的' / '才有辦法找到路徑
參考網站
https://docs.python.org/2/library/webbrowser.html
webbrowser提供簡單的function,來執行瀏覽器
例如
webbrowser.open(url, new=0, autoraise=True)就會打開瀏覽器,瀏覽輸入url的網址的網頁
如果要指定特定瀏覽器來瀏覽
可以使用webbrowser.get來得到object
再使用這個object來呼叫open function
firefox = webbrowser.get('C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s')
firefox.open("www.google.com")
這裡要注意的是傳入get function裡的路徑
與windows裡路徑使用' \ ' 不同
要使用unix-style的' / '才有辦法找到路徑
參考網站
https://docs.python.org/2/library/webbrowser.html
2016年1月23日 星期六
Python try/except
Python的try/excpet是用來測試程式或避免程式錯誤而無法執行
保持之好的習慣,將可能會發生錯誤的程式區塊
加到try/excpet裡,如果這程式區塊在try裡正常達執行
則except區塊會被忽略,反之出現錯誤就會跳到except裡執行
下面是一個簡單的例子,資料型態的轉換
字串轉換成數字,如果字串由數字組成
則程式可正常執行,如果不是就會發生錯誤
num = raw_input('Enter a number:')
try:
my_num = int(num)
print my_num
except:
my_num = 0
print "error input"
保持之好的習慣,將可能會發生錯誤的程式區塊
加到try/excpet裡,如果這程式區塊在try裡正常達執行
則except區塊會被忽略,反之出現錯誤就會跳到except裡執行
下面是一個簡單的例子,資料型態的轉換
字串轉換成數字,如果字串由數字組成
則程式可正常執行,如果不是就會發生錯誤
num = raw_input('Enter a number:')
try:
my_num = int(num)
print my_num
except:
my_num = 0
print "error input"
2016年1月21日 星期四
Python運算元運算規則
Python運算元運算規則,由高到低如下
- 括數(Parenthesis),擁有最高優先權
- 幕次運算(Exponentiation)
- 乘法、除法、和餘數運算
- 加法、減法
- 由左到右運算
Python 保留字
以下是Python是保留字,不能用來當變數的名字。
- and
- del
- for
- is
- raise
- assert
- elif
- from
- lambda
- return
- break
- else
- global
- not
- try
- class
- except
- if
- or
- while
- continue
- exec
- import
- pass
- yield
- def
- finally
- in
- as
- with
Python 變數命名規則
- 第一個字母必須是英文字母或底線_
- 只能由英文字母、數字或底線_,組成變數名稱
- 英文字母大小寫識為不同名稱(Case Sensitive)
合法變數命名: hours、time、_month、days30
錯誤變數命名:24hours、@time、days&30
2015年12月17日 星期四
python宣告變數及刪除變數
python宣告變數非常簡單如下面所示
varibale_name = value
無需在變數名稱前面加上型別,python會根據給的值自已判斷型別。
而不再需要使用變數,可以刪除回收變數,方法如下。
del variable_name
就會刪除變數,再次使用變數的就會出現錯誤。
因為找不到被刪除的變數。
varibale_name = value
無需在變數名稱前面加上型別,python會根據給的值自已判斷型別。
而不再需要使用變數,可以刪除回收變數,方法如下。
del variable_name
就會刪除變數,再次使用變數的就會出現錯誤。
因為找不到被刪除的變數。
訂閱:
文章 (Atom)