Time of Update: 2017-01-18
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: 2017-01-18
1)doctest 使用doctest是一種類似於命令列嘗試的方式,用法很簡單,如下 複製代碼 代碼如下:def f(n): """ >>> f(1) 1 >>> f(2) 2 """ print(n) if __name__ == '__main__': import doctest doctest.testmod()
Time of Update: 2017-01-18
下面是一個實戰項目的結果。 複製代碼 代碼如下:#coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,ImageFilter def initChars(): """ 允許的字元集合,初始集合為數字、大小寫字母 usage: initChars() param: None return: list 返回允許的字元集和 for: picChecker類初始字元集合 todo: Nothing """ nums =
Time of Update: 2017-01-18
self在區分全域變數/函數和對象中的成員變數/函數十分有用。例如,它提供了一種範圍機制,我個人認為比Ruby的@和@@清晰多了,這可能是習慣使然吧,但它確實和C++、Java中的this很相似。 然而,self總是有令我困擾的地方,我以前在這裡說過—我曾幻想能在Python3中這些能得以改進,然後通常會引發一輪熱議並最終以人們所說的“顯勝於隱”告終。 我在巴西的時候曾和Luciano
Time of Update: 2017-01-18
局部名字靜態檢測 Python探測局部範圍的時候:是在python編譯代碼時檢測,而不是通過他們在運行時的賦值。 正常的情況下,沒在函數中複製的名字將在包含它的模組中尋找: >>> x=99 >>> def selector(): ... print x ... >>> selector() 99 但是: >>> def selector(): ... print x ... x=100 ... >>>
Time of Update: 2017-01-18
何為pythonic?pythonic如果翻譯成中文的話就是很python。很+名詞結構的用法在中國不少,比如:很娘,很國足,很CCTV等等。我的理解為,很+名詞表達了一種特殊和強調的意味。所以很python可以理解為:只有python能做到的,區別於其他語言的寫法,其實就是python的慣用和特有寫法。置換兩個變數的值。很python的寫法:複製代碼 代碼如下:a,b = b,a不python的寫法:複製代碼 代碼如下:temp = aa = bb =
Time of Update: 2017-01-18
sqlite3本身並沒有像pymysql一樣原生提供字典形式的遊標。cursor = conn.cursor(pymysql.cursors.DictCursor)但官方文檔裡已經有預留了相應的實現方案。def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d
Time of Update: 2017-01-18
Python爬蟲:一些常用的爬蟲技巧總結爬蟲在開發過程中也有很多複用的過程,這裡總結一下,以後也能省些事情。1、基本抓取網頁get方法import urllib2url "http://www.baidu.com"respons = urllib2.urlopen(url)print response.read()post方法import urllibimport urllib2url = "http://abcde.com"form = {'name':'abc','password':'12
Time of Update: 2017-01-18
前言相信對於python開發人員來說,機器上有不同的python版本是很正常的,因為開發的項目有的用2.6或2.7,有的就要用3.0+版本,如何把這些不同的版本管理好,保持每個環境的乾淨和獨立,方便不同版本之間的切換,這時候就要用到我們的虛擬環境了,所以今天我們就來看看python虛擬環境搭建工具pyenv的使用。1、安裝git clone https://github.com/yyuu/pyenv.git ~/.pyenvecho ‘export
Time of Update: 2017-01-18
初學python,小緣緣出了幾道題: 有一 list a = [1, 2, 3, 4, 5, 6] 請將 a 依 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 列印輸出, 2.將a list 倒序成 [6, 5, 4, 3, 2, 1] 3.將a 中的偶數挑出 *2 ,結果為 [4, 8, 12] 基本上實現: 複製代碼 代碼如下: a=[1,2,3,4,5,6] for i in a: print a.index(i),',',i a.reverse(); print a
Time of Update: 2017-01-18
本文執行個體講述了Python按行讀取檔案的實現方法。分享給大家供大家參考,具體如下:小檔案:#coding=utf-8#author: walker#date: 2013-12-30#function: 按行讀取小檔案all_lines = []try: file = open('txt.txt', 'r') all_lines = file.readlines()except IOError as err: print('File error: ' +
Time of Update: 2017-01-18
想使用Regex來擷取一段文本中的任一字元,寫出如下匹配規則: (.*) 結果運行之後才發現,無法獲得換行之後的文本。於是查了一下手冊,才發現Regex中,“.”(點符號)匹配的是除了分行符號“\n”以外的所有字元。 以下為正確的Regex匹配規則: ([\s\S]*) 同時,也可以用 “([\d\D]*)”、“([\w\W]*)” 來表示。 Web技術之家_www.waweb.cn 在文字檔裡, 這個運算式可以匹配所有的英文 /[ -~]/
Time of Update: 2017-01-18
源碼如下 :儲存成utf-8 bd.py 檔案 複製代碼 代碼如下:#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import urllib ,urllib2 import re def baidu(w): url= "http://www.baidu.com/s?" values = { "w":w.encode('gbk','ignore') } data = urllib.urlencode(values) newurl
Time of Update: 2017-01-18
>>> "hello".encode("hex") '68656c6c6f'相應的還可以>>> '68656c6c6f'.decode("hex") 'hello'查了一下手冊,還有這些codec可用CodecAliasesOperand typePurposebase64_codecbase64, base-64byte stringConvert operand to MIME base64bz2_codecbz2byte stringCompress
Time of Update: 2017-01-18
以前提取這些檔案用的是一同事些的批次檔;用起來不怎麼順手,剛好最近在學些python,所有就自己動手寫了一個python提取檔案的小程式;1、原理 提取檔案的原理很簡單,就是到一個指定的目錄,找出最後修改時間大於給定時間的檔案,然後將他們複製到目標目錄,目標目錄的結構必須和原始目錄一致,這樣工程人員拿到後就可以直接覆蓋整個目錄; 2、實現 為了程式的通用,我定義了下面的設定檔 config.xml複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?
Time of Update: 2017-01-18
代碼如下: 複製代碼 代碼如下:import sys, urllib import datetime,time def getDate(): strday=datetime.datetime.now().__str__() strday=strday.split()[0] return strday #url = "http://www.kingnic.com/list/2009-06-16.txt" def getUrl(dateStr=None): baseUrl ="http://www.
Time of Update: 2017-01-18
1. self, cls 不是關鍵字 在python裡面,self, cls 不是關鍵字,完全可以使用自己寫的任意變數代替實現一樣的效果 代碼1 複製代碼 代碼如下:class MyTest: myname = 'peter' def sayhello(hello): print "say hello to %s" % hello.myname if __name__ == "__main__": MyTest().sayhello() 代碼1中, 用hello代替掉了self,
Time of Update: 2017-01-18
opts, args = getopt.getopt(sys.argv[1:], "t:s:h", ["walletype=", "servicename=",'help']) for a,o in opts: if a in ('-t', '--walletype'): walle_type = o elif a in ('-s', '--servicename'): service_name = o elif a in ('-h', '--help'): help = True 其中t:s:
Time of Update: 2017-01-18
ip正則式為:r'(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])' 以下為一個樣本 #-*- coding:utf-8 -*- import re def ip(): '驗證IP的正則式' def match_group(p): s = '''211.210.209.108 gan ffad1.210.2.108 d ffad1.210.2.109afa''' com =
Time of Update: 2017-01-18
複製代碼 代碼如下: #!/usr/bin/python import sys def left_child(node): return node * 2 + 1 def right_child(node): return node * 2 + 2 def parent(node): if (node % 2): return (i - 1) / 2 else: return (i - 2) / 2 def max_heapify(array, i, heap_size): l =