Time of Update: 2015-08-06
標籤:python linux acceslog 最近面試的一個問題,面的時候腦子卡殼了,面完剛出公司就想起來,很尷尬。於是記錄一下。資料是這樣的:121.31.95.182 - - [06/Aug/2015:08:04:41 +0800] "GET /user.php?do=GetGiftAward&phpp=ANDROID_91&phpl=ZH_CN&pvc=1.5.
Time of Update: 2015-08-07
標籤:chr()、unichr()和ord() chr()函數用一個範圍在range(256)內的(就是0~255)整數作參數,返回一個對應的字元。 unichr()跟它一樣,只不過返回的是 Unicode字元,這個從Python 2.0才加入的unichr()的參數範圍依賴於你的Python是如何被編譯的。如果是配置為USC2的Unicode,那麼它的允許範圍就是 range(65536)或0x0000-0xFFFF;如果配置為UCS4,那麼這個值應該是range(1114112)或
Time of Update: 2015-08-06
標籤:5.1 print和import的更多資訊1. print()3.0之後print不再是語句,而是函數, >>> print(‘udg‘,12,13) udg 12 13 >>> name=‘yanliang‘ >>> print(name) yanliang 2.
Time of Update: 2015-08-05
標籤:logging python 異常處理 import loggingimport osimport timedef logger(user_name): "日誌功能模組" logger = logging.getLogger(user_na
Time of Update: 2015-08-04
標籤:Python的資料類型包括基本的資料類型,如:整型,浮點型,字串,字串和進階資料類型,如列表,元組,字典,Set,Forzensets。 一.整型 Python可以處理任意大小的整數,包括負整數。在python 2.x中,有兩種整數類型,一般的32位整數和長整數,長整數都是以L或者l表示,超過32位後會自動轉換為長整形。在python 3.x中,只有一種類型,沒有長度限制。 在Python 2.x中,表示八進位有兩種方式,一是和其他的程式設計語言一樣,直接在數字前加0,比如010,
Time of Update: 2015-08-04
標籤:先看下面這段代碼,顯然無法work. 因為代碼試圖在TestVariableScope()中引用一個沒有被定義的變數a.所以必須報錯,如-1.不過如果你將第2行代碼注釋掉。代碼就能跑通了,-2。問題1來了:TestVariableScope.a 不是也沒有被定義嗎,為什麼可以work呢?解釋如下:先看代碼第8行,TestVariableScope.a在SetVariable方法中被定義了,SetVariable()又
Time of Update: 2015-08-05
標籤:使用python檢測一個裝置是否ping的通一,subprocess以及常用的封裝函數運行python的時候,我們都是在建立並運行一個進程。像Linux進程那樣,一個進程可以fork一個子進程,並讓這個子進程exec另外一個程式。在Python中,我們通過標準庫中的subprocess包來fork一個子進程,並運行一個外部的程式。subprocess
Time of Update: 2015-08-04
標籤:python如果我們要使用一個package中定義的一個module,有兩種匯入方式方法1:from package import module 方法2:import package import
Time of Update: 2015-08-04
標籤: 一般我們會將自己寫的python模組與python內建的模組分開存放以達到便於維護的目的。 Python 運行環境在尋找模組時是對 sys.path 列表進行遍曆,如果我們想在運行環境中添加自訂的模組,主要有以下三種方法: 1.在sys.path列表中添加新的路徑(只能對執行了sys.path.append命令的當前Python運行環境起作用,對其他運行環境不起作用,也即“一次性”的)。 >>> import sys >>>
Time of Update: 2015-08-05
標籤: str.format() :對應取值name="chenshan"age=30address="宜山路926號新思大廈15樓"print "my name is {},age is {},and address {}".format(name,age,address)print "my name is {name},age is {age},and address {address}".format(name="chenshan",age=30,address=
Time of Update: 2015-08-04
標籤:python如何讓自己編寫的模組可用?三種方法:放在合適的位置 比如降自己編寫的python模組放在dist-packages目錄下添加解譯器的尋找目錄 我們將自己建立的目錄/root/python添加到瞭解釋器的尋找目錄中 這個方法的問題是,當我們重新開啟解譯器,之前添加的目錄會消失添加環境變數:可以使用export PYTHONPATH=$PYTHONPATH:~/python將目前使用者目錄下的python檔案夾添加到環境變數中
Time of Update: 2015-08-05
標籤:這兩個基本上都是在迴圈的時候用。for i in range(0, 100):print i for i in xrange(0, 100):print i這兩個輸出的結果都是一樣的,實際上有很多不同,range會直接產生一個list對象:a = range(0,100)print type(a)print aprint a[0], a[1]而xrange則不會直接產生一個list,而是每次調用返回其中的一個值a = xrange(0,100)print type(a)print
Time of Update: 2015-08-04
標籤:1、安裝環境 window使用者下載 python :https://www.python.org/downloads/release/python-2710/ 安裝不解釋。。 配置環境變數 找到 Path 變數 添加你的 python 安裝目錄 一般是 C:\\python2;C:\\python2\lib; mac 使用者請忽略(系統內建)2、檢測是否安裝 pip 如果沒有安裝pip 請安裝pip(window 請參考
Time of Update: 2015-08-04
標籤:學習Python時,第一個程式hello.py(如下)print("hello welcome to python
Time of Update: 2015-08-03
標籤:# -*- coding: utf-8 –*-目的是告訴Python解譯器,用UTF-8編碼讀取原始碼。然後用Notepad++ 另存新檔... 並選擇UTF-8格式儲存。print u‘中文‘(u和中文之間沒有空格)print會依次列印每個字串,遇到逗號“,”會輸出一個空格,因此,輸出的字串是這樣拼起來的:Python字串用\進行轉義。但是r‘...‘標記法不能表示多行字串,也不能表示包含‘和
Time of Update: 2015-08-02
標籤:字典 字典是Python唯一內建的數學映射類型,字典中的值沒有特殊的順序,鍵可以是數字,字串,甚至是元組字典的建立: 字典由索引值對構成,字典中鍵是唯一的,而值不唯一。>>> a_map={"a":1,"b":2,"c":2} >>> a_map["a"] dict函數來建立字典:
Time of Update: 2015-07-31
標籤:參考李航《統計學習方法》 一開始的感知機章節,看著不太複雜就實現一下。。。 1 """ 2 感知機學習演算法的原始形式 3 例2.1 4 """ 5 import numpy as np 6 7 class Perceptron: 8 def __init__(self,w,b,alpha): 9 self.w = w10 self.b = b11 self.alpha = alpha12 13 def loss(self,
Time of Update: 2015-08-01
標籤:1)關於with...as用的用法,這個主要是用於替換try...finally; 更多可以參見:理解Python中的with…as…文法範例程式碼:try: with open("foo.txt", "w+") as fo: fo.write("www.iptython.me"); print(fo.closed);except IOError as err: print("file error:\n" + str(err));
Time of Update: 2015-07-31
標籤:@[深入Python]__new__和__init__12345678class A(object): def __init__(self): print "init" def __new__(cls,*args, **kwargs):
Time of Update: 2015-07-29
標籤:python web spider 網路爬蟲 Python寫的Web spider:<span style="font-size:14px;"># web spider# author vince 2015/7/29import urllib2import re# get href contentpattern = '<a(?:\\s+.+?)