Time of Update: 2016-06-16
例子:複製代碼 代碼如下:def re_escape(fn): def arg_escaped(this, *args): t = [isinstance(a, VerEx) and a.s or re.escape(str(a)) for a in args] return fn(this, *t) return arg_escaped函數嵌套python允許在定義函數的時候,其函數體內又包含另外一個函數的完整定義,這就是我們通常所說的嵌套定義。為什嗎?
Time of Update: 2016-06-16
近期下載了很多各種教程, 但是不幸的是尾碼名都是 ".mp4", 而本人喜歡 ".rmvb" 尾碼,由於有輕微潔癖, 受不了後面的 ".mp4" 綴, 但是手動修改又太過繁瑣, 所以用近期剛學的 Python 來偷懶吧 ! : )為程式運行前的檔案名稱我們要做的呢, 就是在目前的目錄下,建立一個python檔案, 如 demo2.py 然後用編輯器開啟敲入如下代碼: 複製代碼 代碼如下:import os# 列出目前的目錄下所有的檔案files = os.listdir(".")
Time of Update: 2016-06-16
複製代碼 代碼如下:from selenium import seleniumclass MySpider(CrawlSpider): name = 'cnbeta' allowed_domains = ['cnbeta.com'] start_urls = ['http://www.jb51.net'] rules = ( # Extract links matching 'category.php' (but not matching
Time of Update: 2016-06-16
一:簡介由paramiko是用python語言寫的一個模組,遵循SSH2協議,支援以加密和認證的方式,進行遠程伺服器的串連。由於使用的是python這樣的能夠跨平台啟動並執行語言,所以所有python支援的平台,如Linux, Solaris, BSD, MacOS X,
Time of Update: 2016-06-16
要在使用者瀏覽器上安裝cookie,HTTP伺服器向HTTP響應添加類似以下內容的HTTP前序:複製代碼 代碼如下:Set-Cookie:session=8345234;expires=Sun,15-Nov-2013 15:00:00
Time of Update: 2016-06-16
Linux伺服器有CentOS、Fedora等,都預先安裝了Python,版本從2.4到2.5不等,而Windows類型的伺服器也多數安裝了Python,因此只要在本機寫好一個指令碼,上傳到對應機器,在運行時修改參數即可。Python操作檔案和檔案夾使用的是os庫,下面的代碼中主要用到了幾個函數:os.listdir:列出目錄下的檔案和檔案夾os.path.join:拼接得到一個檔案/檔案夾的全路徑os.path.isfile:判斷是否是檔案os.path.splitext:從名稱中取出一個子部
Time of Update: 2016-06-16
複製代碼 代碼如下:#! /usr/bin/env python#coding=utf-8#實現雜湊表(線性地址再散列)def ChangeKey(key,m,di): key01=(key+di) % m return key01a=raw_input("Please entry the numbers:\n").split()m=len(a)dict01={}for i in a: key=int(i)%m if "%s"%key in dict01:
Time of Update: 2016-06-16
python實現兩個文本合并employee檔案中記錄了工號和姓名複製代碼 代碼如下:cat employee.txt:100 Jason Smith200 John Doe300 Sanjay Gupta400 Ashok Sharmabonus檔案中記錄工號和工資複製代碼 代碼如下:cat bonus.txt:100 $5,000200 $500300 $3,000400 $1,250要求把兩個檔案合并並輸出如下, 處理結果:複製代碼 代碼如下:400 ashok sharma $1,250
Time of Update: 2016-06-16
複製代碼 代碼如下:#! /usr/bin/python''' File : testHash.py Author : Mike'''import hashlibsrc = raw_input("Input string : ")funcNameList = ["MD5","SHA1","SHA224","SHA256","SHA384","SHA512"]funcMap = {"MD5" : lambda cnt :
Time of Update: 2016-06-16
一 、python模組 xml.dom 解析XML的APIminidom.parse(filename)載入讀取XML檔案doc.documentElement擷取XML文檔對象node.getAttribute(AttributeName)擷取XML節點屬性值node.getElementsByTagName(TagName)擷取XML節點對象集合node.childNodes
Time of Update: 2016-06-16
刪除檔案複製代碼 代碼如下:os.remove( filename ) # filename: "要刪除的檔案名稱"產生異常的可能原因:(1)filename 不存在(2)對filename檔案, 沒有操作許可權或唯讀。刪除檔案夾下所有檔案和子檔案夾 :複製代碼 代碼如下:import os def delete_file_folder(src): '''delete files and folders''' if os.path.isfile(src):
Time of Update: 2016-06-16
第一種方法:遞迴複製代碼 代碼如下:def perms(elements): if len(elements) yield elements else: for perm in perms(elements[1:]): for i in range(len(elements)): yield perm[:i] + elements[0:1] + perm[i:]for item in
Time of Update: 2016-06-16
對python網頁請求模組urllib2進行簡單的封裝。例子:複製代碼 代碼如下:#!/usr/bin/python#coding: utf-8import base64import urllibimport urllib2import timeclass SendRequest: ''' This class use to set and request the http, and get the info of response. e.g. set Authorization
Time of Update: 2016-06-16
下面是規則.你和你的對手,在同一時間做出特定的手勢,必須是下面一種手勢:石頭,剪子,布.勝利者從下面的規則中產生,這個規則本身是個悖論.(a) 布包石頭.(b)石頭砸剪子,(c)剪子剪破布.在你的電腦版本中,使用者輸入她/他的選項,電腦找一個隨機選項,然後由你的程式來決定一個勝利者或者平手.注意:最好的演算法是盡量少的使用 if 語句複製代碼 代碼如下:#coding:utf-8import randomguess_list = ["石頭","剪刀","布"]guize = [["布","石頭"
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env python# coding: u8import osimport zbarimport Imageimport urllibimport uuiddef qrRead(url):uuid1 = uuid.uuid1()filename=str(uuid1)+".jpg"print uuid1 urllib.urlretrieve(url, filename)# create a readerscanner =
Time of Update: 2016-06-16
python爬蟲_採集聯想詞代碼複製代碼 代碼如下:#coding:utf-8import urllib2import urllibimport reimport timefrom random import choice#特別提示,下面這個list中的代理ip可能失效,請換上有效代理ipiplist = ['27.24.158.153:81','46.209.70.74:8080','60.29.255.88:8888']list1 = ["集團","科技"]for item in
Time of Update: 2016-06-16
這幾天用到了raw socket,用python寫了些demo程式,這裡記錄下。首先我們看一個簡單的sniffer程式:複製代碼 代碼如下:#! /usr/bin/python# code for linuximport socket#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)s = socket.socket(socket.AF_INET, socket.SOCK_RAW,
Time of Update: 2016-06-16
今天在看其他同事的代碼時,發現一個沒使用過的python關鍵字 :yield 先問了一下同事,聽他說了幾句,有個模糊的印象,僅僅是模糊而已。於是自己去搜搜資料看。看了半天,逐漸清晰了。不過在工作機制以及應用上還是有點迷茫。嗯,先把初始接觸的印象記下來吧。 yield 簡單說來就是一個產生器(Generator)。產生器是這樣一個函數:它記住上一次返回時在函數體中的位置。對產生器函數的第二次(或第 n 次)調用跳轉至該函數中間,而上次調用的所有局部變數都保持不變。
Time of Update: 2016-06-16
urllib2.urlopen()函數不支援驗證、cookie或者其它HTTP進階功能。要支援這些功能,必須使用build_opener()函數建立自訂Opener對象。複製代碼 代碼如下:build_opener([handler1 [ handler2, ... ]])參數handler是Handler執行個體,常用的有HTTPBasicAuthHandler、HTTPCookieProcessor、ProxyHandler等。build_opener
Time of Update: 2016-06-16
最近遇到了Python訪問SqlServer的問題,這裡總結下。一、Windows下配置Python訪問Sqlserver環境:Windows 7 + Sqlserver 20081、下載並安裝pyodbc下載地址:http://code.google.com/p/pyodbc/downloads/list2、訪問SqlServer複製代碼 代碼如下:>>> import pyodbc>>>cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192