Time of Update: 2016-06-10
假設定義了一個類:C,該類必須繼承自object類,有一私人變數_x複製代碼 代碼如下:class C: def __init__(self): self.__x=None 1.現在介紹第一種使用屬性的方法: 在該類中定義三個函數,分別用作賦值、取值和刪除變數(此處表達也許不很清晰,請看樣本) def getx(self): return self.__x def setx(self,value): self.__x=value def delx(self): del self.__
Time of Update: 2016-06-10
本文執行個體講述了python尋找指定具有相同內容檔案的方法。分享給大家供大家參考。具體如下:python代碼用於尋找指定具有相同內容的檔案,可以同時指定多個目錄調用方式:python doublesdetector.py c:\;d:\;e:\ > doubles.txt# Hello, this script is written in Python - http://www.python.org# doublesdetector.py 1.0pimport os, os.path,
Time of Update: 2016-06-10
本文執行個體講述了python連結Oracle資料庫的方法。分享給大家供大家參考。具體如下:這裡使用python連結Oracle資料庫需要引用cx_Oracle庫#coding=UTF-8 import cx_Oracle def hello(): '''Hello cx_Oracle樣本: 1)列印資料庫版本資訊. 2)查詢表資料.''' conn = cx_Oracle.connect("obs61","obs61","tx8i.hp") cur =
Time of Update: 2016-06-10
本文執行個體講述了python實現的簡單FTP上傳下載檔案的方法。分享給大家供大家參考。具體如下:python本身內建一個FTP模組,可以實現上傳下載的函數功能。#!/usr/bin/env python # -*- coding: utf-8 -*- from ftplib import FTP def ftp_up(filename = "20120904.rar"): ftp=FTP() ftp.set_debuglevel(2)
Time of Update: 2016-06-10
本文執行個體講述了python實現中文分詞FMM演算法。分享給大家供大家參考。具體分析如下:FMM演算法的最簡單思想是使用貪心演算法向前找n個,如果這n個組成的詞在詞典中出現,就ok,如果沒有出現,那麼找n-1個...然後繼續下去。假如n個詞在詞典中出現,那麼從n+1位置繼續找下去,直到句子結束。import re def PreProcess(sentence,edcode="utf-8"): sentence = sentence.decode(edcode)
Time of Update: 2016-06-10
本文執行個體講述了python實現檔案快照加密保護的方法。分享給大家供大家參考。具體如下:這段代碼可以對指定的目錄進行掃描,包含子目錄,對指定副檔名的檔案進行SHA-1加密後儲存在cvs檔案,以防止檔案被篡改調用方法:python snapper.py > todayCheck.csv# Hello, this is a script written in Python. See http://www.pyhon.org## Snapper 1.2p## This script will
Time of Update: 2016-06-10
本文執行個體講述了python根據日期返回星期幾的方法。分享給大家供大家參考。具體如下:這個函數給定日期,輸出星期幾,至於0是星期一還是星期天,這和時區有關,反正我這裡是0表示星期一import time,datetimedef get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三', 3 : '星期四', 4 : '星期五', 5 : '星期六', 6 : '星期天'
Time of Update: 2016-06-10
本文執行個體講述了python清除字串裡非字母字元的方法。分享給大家供大家參考。具體如下:s = "hello world! how are you? 0" # Short version print filter(lambda c: c.isalpha(), s) # Faster version for long ASCII strings: id_tab = "".join(map(chr, xrange(256))) tostrip = "".join(c for c in
Time of Update: 2016-06-10
本文執行個體講述了Python抓取百度查詢結果的方法。分享給大家供大家參考。具體實現方法如下:#win python 2.7.ximport re,sys,urllib,codecsxh = urllib.urlopen("http://www.baidu.com/s?q1=123&rn=100").read().decode('utf-8') rc = re.compile(r'(?P.*?)',re.I)match = rc.finditer(xh)rcr = re.
Time of Update: 2016-06-10
本文執行個體講述了Python中max函數用法。分享給大家供大家參考。具體如下:這裡max函數是Python內建的函數,不需要匯入math模組# 最簡單的max(1, 2)max('a', 'b')# 也可以對列表和元組使用max([1,2])max((1,2))# 還可以指定comparator functionmax('ah', 'bf', key=lambda x: x[1])def comparator(x): return x[1]max('ah', 'bf',
Time of Update: 2016-06-10
在我們想要捕獲的URL部分上加上小括弧,Django 會將捕獲的文本作為位置參數傳遞給視圖函數。 在更進階的用法中,還可以使用 命名 Regex組來捕獲URL,並且將其作為關鍵字參數傳給視圖。一個 Python 函數可以使用關鍵字參數或位置參數來調用,在某些情況下,可以同時進行使用。 在關鍵字參數調用中,你要指定參數的名字和傳入的值。 在位置參數調用中,你只需傳入參數,不需要明確指明哪個參數與哪個值對應,它們的對應關係隱含在參數的順序中。例如,考慮這個簡單的函數:def sell(item,
Time of Update: 2016-06-10
本文執行個體講述了python冒泡排序簡單實現方法。分享給大家供大家參考。具體實現方法如下:#!/usr/bin/pythonimport randomdef bubble_sort(data): length = len(data) for i in range(len(data) - 1): for j in range(len(data) - 1): if (data[j] 運行結果如下:[115, 14, 246, 125, 94, 78, 275, 163, 64, 72
Time of Update: 2016-06-10
好久沒寫這系列的文章了,我越來越喜歡用python了,它在我的工作中佔據的比例越來越大。廢話少說,直接進入主題。 anydbm允許我們將一個磁碟上的檔案與一個“dict-like”對象關聯起來,操作這個“dict-like”對象,就像操作dict對象一樣,最後可以將“dict-like”的資料持久化到檔案。對這個”dict-like”對象進行操作的時候,key和value的類型必須是字串。下面是使用anydbm的例子: #coding=utf-8 import anydbm def
Time of Update: 2016-06-10
本文執行個體講述了Python字串匹配演算法KMP。分享給大家供大家參考。具體如下:#!/usr/bin/env python#encoding:utf8def next(pattern):p_len = len(pattern)pos = [-1]*p_lenj = -1for i in range(1, p_len):while j > -1 and pattern[j+1] != pattern[i]:j = pos[j]if pattern[j+1] == pattern[i]:j =
Time of Update: 2016-06-10
本文執行個體講述了python實現使用者登陸郵件通知的方法。分享給大家供大家參考。具體如下:這裡寫在linux計劃任務裡定時執行,當有新使用者登陸時候發送使用者名稱到指定郵箱通知管理員。#!/usr/bin/env python#encoding=utf-8from smtplib import SMTPimport subprocesssmtp = "smtp.qq.com"user = '1234567'password = 'xxxx'run_comd =
Time of Update: 2016-06-10
本文執行個體講述了Python本地與全域命名空間用法。分享給大家供大家參考。具體如下:x = 1def fun(a): b=3 x=4 def sub(c): d=b global x x = 7 print ("Nested Function\n=================") print locals() sub(5) print ("\nFunction\n=================") print locals() print
Time of Update: 2016-06-10
Python提供了兩個內建函數從標準輸入讀入一行文本,預設的標準輸入是鍵盤。如下:1.raw_input2.inputraw_input函數raw_input() 函數從標準輸入讀取一個行,並返回一個字串(去掉結尾的分行符號):複製代碼 代碼如下:str = raw_input("Enter your input: "); print "Received input is : ", str 這將提示你輸入任一字元串,然後在螢幕上顯示相同的字串。當我輸入"Hello
Time of Update: 2016-06-10
本文執行個體講述了Python實現大檔案排序的方法。分享給大家供大家參考。具體實現方法如下:import gzipimport osfrom multiprocessing import Process, Queue, Pipe, current_process, freeze_supportfrom datetime import datetimedef sort_worker(input,output): while True: lines =
Time of Update: 2016-06-10
Python是一門非常簡潔的語言,對於資料類型的表示,不像其他語言預定義了許多類型(如:在C#中,光整型就定義了8種),它只定義了六種基本類型:字串,整數,浮點數,元組,列表,字典。通過這六種資料類型,我們可以完成大部分工作。但當Python需要通過網路與其他的平台進行互動的時候,必須考慮到將這些資料類型與其他平台或語言之間的類型進行互相轉換問題。打個比方:C++寫的用戶端發送一個int型(4位元組)變數的資料到Python寫的伺服器,Python接收到表示這個整數的4個位元組資料,怎麼解析成P
Time of Update: 2016-06-10
本文執行個體講述了python基本進位轉換的方法。分享給大家供大家參考。具體如下:# Parsing string with base into a number is easynum = int(str, radix)# We have to write our own function for outputting to string with arbitrary basedef itoa(num, radix): result = "" while num > 0: result =