Time of Update: 2018-12-04
無數人寫過 blog / post 關於這個問題,但我還是又重新解決了一遍。先公布答案。 1、MySQLdb 連線類型conn = MySQLdb.connect(..., use_unicode = True, charset = "utf8",) 2、cursor.fetchall() 得到的字串 typeof() 得到 <type 'unicode'>,所以要串接,要全部使用 unicodefor x in cursor.fetchall(): line = u"%s %
Time of Update: 2018-12-04
from os.path import basenamefrom urlparse import urlsplitimport urllib,urllib2def url2name(url): return basename(urlsplit(url)[2])def download(url, localFileName = None): localName = url2name(url) req = urllib2.Request(url,
Time of Update: 2018-12-04
在運行這樣類似的代碼:#!/usr/bin/env python s="中文" print s最近經常遇到這樣的問題:問題一:SyntaxError: Non-ASCII character '\xe4' in file E:\coding\python\Untitled 6.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for
Time of Update: 2018-12-04
唉~今天為了python中靜態方法和類方法差了一下午啊,最後在官方文檔中糾結了半天,最後終於知道了這兩種函數有什麼區別。唉~怎麼這麼糾結啊~果然對一門語言深入學習是很蛋疼的事情啊,還好暫時不打算深入學習python。
Time of Update: 2018-12-04
最近需要操作inf格式文檔,用原本的文本讀取方式不成功,於是搜尋了一下找到原因。需要讀取的目錄下的檔案有兩種編碼方式,一種是ANSI,另外一種是Unicode,但是Unicode的儲存方式有UTF-8,UTF-16等,UTF即為Unicode Translation
Time of Update: 2018-12-04
一個簡單的ftp程式,實現從伺服器下載檔案和上傳檔案#!/usr/bin/env pythonimport sysimport osfrom ftplib import FTPimport timedef ftp_down(): hftp = FTP('ip') hftp.login('user','passwd') hftp.cwd('serverpath') os.chdir('localpath') flst = hftp.nlst() for
Time of Update: 2018-12-04
Recipe 1.9. Simplifying Usage of Strings' translate MethodRecipe 1.9. 簡化字串 translate 方法的用法Credit: Chris Perkins, Raymond Hettinger問題您常常希望使用高效的字串 translate 方法,但卻很難記住該方法和 string.maketrans 函數的詳細用法,因此您想要一種能在通常情況下簡化其用法的封裝方案(facade)。解法正如 Recipe 1.10
Time of Update: 2018-12-04
將以下代碼粘貼在ultraedit安裝目錄下的wordfile.txt裡面就可以了。/L10"Python" PYTHON_LANG Line Comment = # Escape Char = \ File Extensions = PY PYW/Indent Strings = ":"/Function String 1 = "%[ ,^t]++def[ ]+^([a-zA-Z0-9_]+*^):"/Function String 2 = "%[ ,^t]++^(class[
Time of Update: 2018-12-04
關於 Python 代碼最佳化你需要知道的最重要問題是,決不要自己編寫計時函數。 為一個很短的代碼計時都很複雜。處理器有多少時間用於運行這個代碼?有什麼在後台運行嗎?每個現代電腦都在後台運行持續或者間歇的程式。小小的疏忽可能破壞你的百年大計,後台服務偶爾被 “喚醒”
Time of Update: 2018-12-04
[Python]在cygwin下編譯MySQLdb for python1. 準備2. 編譯安裝MySQLClient3. 編譯安裝MySQLdb3.1. 解壓3.2. 編譯前的設定3.3.
Time of Update: 2018-12-04
不知道有多少人在關注 django 和學習 Django ,那麼從文檔入手是必要的。不過 Django 提供的文檔都在 docs 目錄,而且是 rest 格式寫的,那麼如何轉換呢。其實很簡單,如果你下載了 docutils 包,解壓後在 tools 目錄下可以看到一個叫 buildhtml.py 的檔案,將這個檔案拷貝到 docs 目錄下,然後進入命令列執行:Python buildhtml.py那麼這個程式就會將目前的目錄下的文字檔自動轉為 html 檔案。怎麼樣,簡單吧。 =========
Time of Update: 2018-12-04
以前連接字串喜歡用"%s%s"方式 發現效率很低 不過又有更好的方法了 1 2 3 4 5 6 7 8 91011121314151617import timedef main1():st=time.clock()s=""for x in range(20000):s="%s%s"%(s,x)teststr=sprint "main1 len=%s time:%s"%(len(teststr),time.clock()-st)def
Time of Update: 2018-12-04
讀書筆記: 精通Regextxt檔案#mail.txt From Xihaode o dfsfsa Received: womenshiw To: xiaopengyou@126.com (Xiao Peng)From: dapengyou@qq.com (Da Peng)Date: 05/02/2000 at 04:58:50Subject: Re: Using the mod() function with negative numbersThanks very much for
Time of Update: 2018-12-04
沒有考慮效率問題,只是為了簡潔,呵呵#問題2 使用filter講100以內的質數挑選出來#filter(function or None, sequence) -> list, tuple, or string #filter(def1,list)->list就是2,到100了,這個def怎麼辦呢 就是一個傳回值為布爾型的函數def isprime(m): #質數是指只能被1和它本身整除的數 for i in range(2, m/2+1): if m %
Time of Update: 2018-12-04
代碼: 用戶端:#!/usr/bin/env python#coding:utf8 #python2.73 lession7_1_s.py"""這是一個多用戶端 用來測試 有5個使用者 7個訊息 一共發送35次 """import socketfrom time import sleepmsg = ['cs111','cs222','cs333','cs444','cs555','cs666','cs777']ss = [] #用戶端集合saddr = ('localhost',30001)
Time of Update: 2018-12-04
#coding=utf8#graptest1.py python2.7 2013.4.5'''單頁裡的列表匹配,單線程下載,html檔案格式 完成一個原型 regex寫的很爛。。'''import urllib,urllib2import re,codecsurl =r'http://hi.baidu.com'user = r'/lzz847915049'page = urllib2.urlopen(url+user)print url+userpagecontent =
Time of Update: 2018-12-04
1.More about Print print 'Age:', 42 ---> Age: 42 注意以下語句的區別: 1, 2, 3 ---> (1, 2, 3) print 1, 2, 3 ---> 1 2 3 print (1, 2, 3) ---> (1, 2, 3) 2.More about Import import somemodule from somemodule import somefunction from somemodule import
Time of Update: 2018-12-04
0.最重要的內建異常Exception:所有異常的基類AttributeError:屬性引用或賦值異常IOError:試圖讀、寫不存在的檔案IndexError:使用Sequence中不存在的IndexKeyError:使用Mapping中不存在的KeyNameError:變數名未找到SyntaxError:文法錯TypeError:在內建函數中使用錯誤的類型ValueError:類型對,但值不正確ZeroDivisionError:除零1.自訂異常class
Time of Update: 2018-12-04
O'Reilly採訪了Python語言作者Guido van Rossum,討論了Python 3以及未來的發展。在最後,他表示:我想重申一點,決定是使用3.0還是2.6都是個人的選擇。你不會因採取保守立場而有被拋在後面的風險。3.0和2.6都被同一個核心的Python開發人員小組所支援。此時我們也不特彆強調Python
Time of Update: 2018-12-04
工作中需要讀取指定xml資料庫中的資料以及 表所需欄位名,所以在已有例子中改進實現:xml 資料庫 xmldabase.xml:<database><manifest><pair key="schema_major_vsn" value="5"/><pair key="schema_minor_vsn" value="63"/><pair key="generation_count"