日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python開發(fā)者應(yīng)該知道的7個開發(fā)庫

在我多年的 Python 編程經(jīng)歷以及在 Github 上的探索漫游過程中,我發(fā)掘到一些很不錯的 Python 開發(fā)包,這些包大大簡化了開發(fā)過程,而本文就是為了向大家推薦這些開發(fā)包。

十余年的茂名網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整茂名建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“茂名網(wǎng)站設(shè)計”,“茂名網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

請注意我特別排除了像SQLAlchemy和Flask這樣的庫,因為其實在太優(yōu)秀了,無需多提。

下面開始:

1. PyQuery (with lxml)

安裝方法 pip install pyquery

Python 解析 HTML 時最經(jīng)常被推薦的是Beautiful Soup ,而且它的確也表現(xiàn)很好。提供良好的 Python 風(fēng)格的 API,而且很容易在網(wǎng)上找到相關(guān)的資料文檔,但是當(dāng)你需要在短時間內(nèi)解析大量文檔時便會碰到性能的問題,簡單,但是真的非常慢。

下圖是 08 年的一份性能比較圖:

這個圖里我們發(fā)現(xiàn) lxml 的性能是如此之好,不過文檔就很少,而且使用上相當(dāng)?shù)谋孔?!那么是選擇一個使用簡單但是速度奇慢的庫呢,還是選擇一個速度飛快但是用起來巨復(fù)雜的庫呢?

誰說二者一定要選其一呢,我們要的是用起來方便,速度也一樣飛快的 XML/HTML 解析庫!

而 PyQuery 就可以同時滿足你的易用性和解析速度方面的苛刻要求。

看看下面這幾行代碼:

 
 
 
 
  1. from pyquery import PyQuery  
  2. page = PyQuery(some_html)  
  3.  
  4. last_red_anchor = page('#container > a.red:last') 

很簡單吧,很像是 jQuery,但它卻是 Python。

不過也有一些不足,在使用迭代時需要對文本進行重新封裝:

 
 
 
 
  1. for paragraph in page('#container > p'):  
  2.     paragraph = PyQuery(paragraph)  
  3.     text = paragraph.text() 

2. dateutil

安裝方法:pip install dateutil

處理日期很痛苦,多虧有了 dateutil

 
 
 
 
  1. from dateutil.parser import parse  
  2.  
  3. >>> parse('Mon, 11 Jul 2011 10:01:56 +0200 (CEST)')  
  4. datetime.datetime(2011, 7, 11, 10, 1, 56, tzinfo=tzlocal())  
  5.  
  6. # fuzzy ignores unknown tokens  
  7.  
  8. >>> s = """Today is 25 of September of 2003, exactly  
  9. ...        at 10:49:41 with timezone -03:00.""" 
  10. >>> parse(s, fuzzy=True)  
  11. datetime.datetime(2003, 9, 25, 10, 49, 41,  
  12.                   tzinfo=tzoffset(None, -10800)) 

3. fuzzywuzzy

安裝方法:pip install fuzzywuzzy

fuzzywuzzy 可以讓你對兩個字符串進行模糊比較,當(dāng)你需要處理一些人類產(chǎn)生的數(shù)據(jù)時,這非常有用。下面代碼使用Levenshtein 距離比較方法來匹配用戶輸入數(shù)組和可能的選擇。

 
 
 
 
  1. from Levenshtein import distance  
  2.  
  3. countries = ['Canada', 'Antarctica', 'Togo', ...]  
  4.  
  5. def choose_least_distant(element, choices):  
  6.     'Return the one element of choices that is most similar to element' 
  7.     return min(choices, key=lambda s: distance(element, s))  
  8.  
  9. user_input = 'canaderp' 
  10. choose_least_distant(user_input, countries)  
  11. >>> 'Canada' 

這已經(jīng)不錯了,但還可以做的更好:

 
 
 
 
  1. from fuzzywuzzy import process  
  2.  
  3. process.extractOne("canaderp", countries)  
  4. >>> ("Canada", 97) 

4. watchdog

安裝方法:pip install watchdog

watchdog 是一個用來監(jiān)控文件系統(tǒng)事件的 Python API和shell實用工具。

5. sh

安裝方法:pip install sh

sh 可讓你調(diào)用任意程序,就好象是一個函數(shù)一般:

 
 
 
 
  1. from sh import git, ls, wc  
  2.  
  3. # checkout master branch  
  4. git(checkout="master")  
  5.  
  6. # print(the contents of this directory  
  7. print(ls("-l"))  
  8.  
  9. # get the longest line of this file  
  10. longest_line = wc(__file__, "-L") 

6. pattern

安裝方法:pip install pattern

Pattern 是 Python 的一個 Web 數(shù)據(jù)挖掘模塊。可用于數(shù)據(jù)挖掘、自然語言處理、機器學(xué)習(xí)和網(wǎng)絡(luò)分析。

7. path.py

安裝方法:pip install path.py

當(dāng)我開始學(xué)習(xí) Python 時,os.path 是我最不喜歡的 stdlib 的一部分。盡管在一個目錄下創(chuàng)建一組文件很簡單。

 
 
 
 
  1. import os  
  2.  
  3. some_dir = '/some_dir' 
  4. files = []  
  5.  
  6. for f in os.listdir(some_dir):  
  7.     files.append(os.path.joinpath(some_dir, f)) 

但listdir在os而不是os.path中。

而有了path.py ,處理文件路徑變得簡單:

 
 
 
 
  1. from path import path  
  2.  
  3. some_dir = path('/some_dir')  
  4.  
  5. files = some_dir.files() 

其他的用法:

 
 
 
 
  1. >>> path('/').owner  
  2. 'root' 
  3.  
  4. >>> path('a/b/c').splitall()  
  5. [path(''), 'a', 'b', 'c']  
  6.  
  7. # overriding __div__  
  8. >>> path('a') / 'b' / 'c' 
  9. path('a/b/c')  
  10.  
  11. >>> path('ab/c').relpathto('ab/d/f')  
  12. path('../d/f') 

是不是要好很多?

原文鏈接:http://www.oschina.net/question/12_78983


當(dāng)前文章:Python開發(fā)者應(yīng)該知道的7個開發(fā)庫
分享路徑:http://www.dlmjj.cn/article/dheiscj.html