Time of Update: 2016-06-06
一、開啟一個網頁擷取所有的內容from urllib import urlopendoc = urlopen("http://www.baidu.com").read()print doc二、擷取Http頭from urllib import urlopendoc = urlopen("http://www.baidu.com")print doc.info()print doc.info().getheader('Content-Type') 三、使用代理 1. 查看環境變數print
Time of Update: 2016-06-06
>>> a = 2.5>>> b = 2.5>>> c = b>>> a is cFalse>>> a = 2>>> b = 2>>> c = b>>> a is cTrue在使用is函數的時候去列印a,b分別被賦值為2.5 和2的情況,發現:>>> a = 2>>> b = 2>>> id(a)21132060>>> id(b)21132060>>> a = 2.5>>> b = 2.5>>> id(a)19622112>>>
Time of Update: 2016-06-06
self在區分全域變數/函數和對象中的成員變數/函數十分有用。例如,它提供了一種範圍機制,我個人認為比Ruby的@和@@清晰多了,這可能是習慣使然吧,但它確實和C++、Java中的this很相似。 然而,self總是有令我困擾的地方,我以前在這裡說過—我曾幻想能在Python3中這些能得以改進,然後通常會引發一輪熱議並最終以人們所說的“顯勝於隱”告終。 我在巴西的時候曾和Luciano
Time of Update: 2016-06-06
根據本人的學習經驗,我總結了以下十點和大家分享: 1)學好python的第一步,就是馬上到www.python.org網站上下載一個python版本。我建議初學者,不要下載具有IDE功能的整合式開發環境,比如Eclipse外掛程式等。 2)下載完畢後,就可以開始學習了。學習過程中,我建議可以下載一些python的學習文檔,比如《dive into python》,《OReilly - Learning Python》等等。通過學習文法,掌握python中的關鍵字文法,函數文法,數學運算式等等 3
Time of Update: 2016-06-06
下面是一個實戰項目的結果。 代碼如下:#coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,ImageFilter def initChars(): """ 允許的字元集合,初始集合為數字、大小寫字母 usage: initChars() param: None return: list 返回允許的字元集和 for: picChecker類初始字元集合 todo: Nothing """ nums = [str(i)
Time of Update: 2016-06-06
這篇文章是關於 wxPython,但 wxPython 實際是兩件事物的組合體:Python 指令碼語言和 GUI 功能的 wxWindows 庫(關於 wxWindows 的介紹,請參閱 developerWorks上的 “細述 wxWindows” )。wxWindows 庫是為了最大可移植性的 C/C++ 庫,而抽取 GUI 功能。所以 wxWindows 應用程式與生俱來地可以運行在 Windows、帶 X、KDE 或 Gnome 的 UNIX 或者 wxWindows
Time of Update: 2016-06-06
代碼如下:#coding:utf-8 import urllib.request import xml.dom.minidom import sqlite3 import threading import time class logger(object): def log(self,*msg): for i in msg: print(i) Log = logger() Log.log('測試下') class downloader(object): def
Time of Update: 2016-06-06
下面是調用方式:Example script - pymssql module (DB API 2.0) Example script - _mssql module (lower level DB access) 不過,在我使用過程中,發現,如果表中包含了ntext欄位,就會出錯,提示 不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版本將 ntext 資料或僅使用Unicode定序的 Unicode 資料發送到用戶端。查了一下,發現官方網站有解釋:Q: What
Time of Update: 2016-06-06
比如用 print ','.join(datelist) 就可以將datelist列表裡面的所有項目並成一個字串,當然這個運算式會在每一個項目中間插入一個逗號,這種方式比用迴圈的方式更簡潔。 日期的操作必須使用time或datetime庫 import time >>> s="2006-1-2" >>> time.strptime(s,"%Y-%m-%d) 這是將字串格式的日期及時間轉成日期對象 轉義符對應意義如下 %a 本地簡化星期名稱 %A 本地完整星期名稱 %b 本地簡化的月份名稱 %B
Time of Update: 2016-06-06
在Python中是通過一套命名體系來識別成約的存取範圍的 class MyObjec(object): username = "developerworks" # public _email = "developerworks#163#.com" #protected __tel = "1391119****" # private 從這段代碼中可以看出一些巧妙的命名方法
Time of Update: 2016-06-06
比如,若要將某個String對象s從gbk內碼轉換為UTF-8,可以如下操作 s.decode('gbk').encode('utf-8′) 可是,在實際開發中,我發現,這種辦法經常會出現異常: UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence 這
Time of Update: 2016-06-06
1)doctest 使用doctest是一種類似於命令列嘗試的方式,用法很簡單,如下 代碼如下:def f(n): """ >>> f(1) 1 >>> f(2) 2 """ print(n) if __name__ == '__main__': import doctest doctest.testmod() 應該來說是足夠簡單了,另外還有一種方式doctest.testfile(filename),就是把命令列的方式放在檔案裡進行測試。 2)unittest
Time of Update: 2016-06-06
1、hashlib import hashlib #建立一個雜湊對象 md = hashlib.md5() #md = hashlib.sha1() #md = hashlib.sha224() #md = hashlib.sha25() #md = hashlib.sha384() #md = hashlib.sha512() 1.1 hashlib.update(arg) 1.2 hashlib.digest() #返回數字形式的雜湊 1.3 hashlib.hexdigest() #返回1
Time of Update: 2016-06-06
它是Python語言對流行的wxWidgets跨平台GUI工具庫的綁定。而wxWidgets是用C++語言寫成的。 和Python語言與wxWidgets GUI工具庫一樣,wxPython是開源軟體。這意味著任何人都可以免費地使用它並且可以查看和修改它的原始碼,或者貢獻補丁,增加功能。 wxPython是跨平台的。這意味著同一個程式可以不經修改地在多種平台上運行。現今支援的平台有:32位微軟Windows作業系統、大多數Unix或類Unix系統、蘋果Mac OS X。
Time of Update: 2016-06-06
從簡單的角度來看的話,zip格式會是個不錯的選擇,而且python對zip格式的支援夠簡單,夠好用。1)簡單應用 如果你僅僅是希望用python來做壓縮和解壓縮,那麼就不用去翻文檔了,這裡提供一個簡單的用法,讓你一看就能明白。 import zipfile f = zipfile.ZipFile('filename.zip', 'w' ,zipfile.ZIP_DEFLATED) f.write('file1.txt') f.write('file2.doc')
Time of Update: 2016-06-06
後來想到自己Delphi有一個獲得拼音的代碼。於是找了出來。研究了一下代碼如下: 代碼如下:function get_hz_pywb(hzstr: string; pytype: integer): string; var I: Integer; allstr: string; hh: THandle; pp: pointer; ss: TStringList; function retturn_wbpy(tempstr: string; tqtype: integer): string;
Time of Update: 2016-06-06
IDLE 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3, 'bb': 2} >>> for k in sorted(dic.keys()): print (k) aa ab ----------------------------------------------- 字典對象其實就是鍵-值對 下面是字典對象的添加,修改,刪除 (修改與添加方法相同,當key值不存在的時候添加) >>> dic["cc"] = 4 >
Time of Update: 2016-06-06
沒辦法就下了一個2.6,如果用2.4就太低了,又折騰了,半天找到了MySQL-python-1.2.2.win32-py2.6.exe 這個安裝檔案,安裝完成,執行 import MySQLdb 出現問題提示: File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 19, in ImportError: DLL load failed: 找不到指定的模組。 經過無數的尋找,發現官方的說法也也不是很正確,官方給的解決辦法是
Time of Update: 2016-06-06
1)os.path 1.1 os.path.isabs(path) 是否是絕對路徑 1.2 os.path.isfile(path) 1.3 os.path.isdir(path) 1.4 os.path.islink(path) 是否是連結;但如果系統不支援連結,返回False 1.5 os.path.ismount(path) 是否為磁碟機;但是很不幸的是在python 3.0中這是個不能啟動並執行函數。 原函數如下: # Is a path a mount point? Either a
Time of Update: 2016-06-06
首先下載 MySQLdb #encoding=GBK import MySQLdb #import sys # #reload(sys) #sys.setdefaultencoding('utf-8') print 'Connection ...' host='192.168.1.77' user='root' passwd='123456' db='test' conn = MySQLdb.connect(host,user,passwd,db,charset='gbk') print