1、python split()函數使用拆分字符串 將字符串轉化為列表
>>> u = "www.doiido.com.cn"#使用默認分隔符>>> PRint u.split()['www.doiido.com.cn']#以"."為分隔符>>> print u.split('.')['www', 'doiido', 'com', 'cn']2、使用Python解析JSON數據的基本方法 從網頁上下載json文件html,提出里面的title。
import jsonjsonStr = json.loads(html)title=jsonStr['title']3、Python判斷文件和文件夾是否存在的方法
>>> import os>>> os.path.exists('d:/assist')True>>> os.path.exists('d:/assist/getTeacherList.py')True>>> os.path.isfile('d:/assist')False>>> os.path.isfile('d:/assist/getTeacherList.py')True>>> os.makedirs('d:/assist/set')>>> os.path.exists('d:/assist/set')True4、Python教程:[61]創建文件夾/目錄 判斷’../src/temp/’文件夾是否存在,如果沒有則建立相應文件夾
import osif os.path.exists('../src/temp/')==False: os.mkdir('../src/temp/')如果是多級目錄,用os.makedirs()函數 5、Python爬蟲關于urlretrieve()函數的使用筆記
import urllibdef cbk(a, b, c): '''回調函數 @a: 已經下載的數據塊 @b: 數據塊的大小 @c: 遠程文件的大小 ''' per = 100.0 * a * b / c if per > 100: per = 100 print '%.2f%%' % perurl = 'http://www.google.com'local = 'd://google.html'urllib.urlretrieve(url, local, cbk)url:遠程文件地址 local:本地保存路徑 cbk:會掉函數,顯示下載進度
6、 python 3.3 try..catch 小例
s=input("input your age:") if s=="": raise Exception("input must not be empty.") try: i=int(s) except Exception as err: print(err) finally: print("Goodbye!")7、 Python中函數參數(默認、列表、可變長度、字典類型)
新聞熱點
疑難解答