惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
Project Zero
Project Zero
D
DataBreaches.Net
I
InfoQ
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Vercel News
Vercel News
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
The Hacker News
The Hacker News
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
T
Threatpost
爱范儿
爱范儿
N
Netflix TechBlog - Medium
D
Docker
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
K
Kaspersky official blog
H
Help Net Security
S
Secure Thoughts
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
G
Google Developers Blog
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
B
Blog
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
P
Privacy International News Feed
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog

博客园 - analyzer

46 个非常有用的 PHP 代码片段 加载 CSS 时不影响页面渲染 21副GIF动图让你了解各种数学概念 各种有用的PHP开源库精心收集 10 个超酷的加载中的 Gif 动画 国内CDN公共库 http://www.cnbeta.com/articles/306769.htm 玩转WIN7的MKLINK 盘点国内网站常用的一些 CDN 公共库加速服务 15 个很棒的 Bootstrap UI 界面编辑器 2014 年 20 款最好的 CSS 工具 Web 开发中 20 个很有用的 CSS 库 20+ 个很有用的 jQuery 的 Google 地图插件 12 个 Web 设计师必备的 Bootstrap 工具 20 个免费的 Bootstrap 的后台管理模板 20 个强大的 Sublime Text 插件 网页设计:使用 CSS3 Box Shadow 实现的 10 个创新技术 CSS工具汇总 输入法漏洞再现Windows 8 利用QQ拼音纯净版实现提权
Python 开发者应该知道的 7 个开发库
analyzer · 2012-11-18 · via 博客园 - analyzer

Posted on 2012-11-18 22:48  analyzer  阅读(575)  评论()    收藏  举报


本文由 OSChina 译自 7 Python Libraries you should know about

在我多年的 Python 编程经历以及在 Github 上的探索漫游过程中,我发掘到一些很不错的 Python 开发包,这些包大大简化了开发过程,而本文就是为了向大家推荐这些开发包。

请注意我特别排除了像 SQLAlchemy 和 Flask 这样的库,因为其实在太优秀了,无需多提。

下面开始:

1. PyQuery (with lxml)

安装方法 pip install pyquery

Python 解析 HTML 时最经常被推荐的是 Beautiful Soup ,而且它的确也表现很好。提供良好的 Python 风格的 API,而且很容易在网上找到相关的资料文档,但是当你需要在短时间内解析大量文档时便会碰到性能的问题,简单,但是真的非常慢。

下图是 08 年的一份性能比较图:

http://blog.ianbicking.org/wp-content/uploads/images/parsing-results.png

这个图里我们发现 lxml 的性能是如此之好,不过文档就很少,而且使用上相当的笨拙!那么是选择一个使用简单但是速度奇慢的库呢,还是选择一个速度飞快但是用起来巨复杂的库呢?

谁说二者一定要选其一呢,我们要的是用起来方便,速度也一样飞快的 XML/HTML 解析库!

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

看看下面这几行代码:

1from pyquery import PyQuery
2page = PyQuery(some_html)
4last_red_anchor = page('#container > a.red:last')
很简单吧,很像是 jQuery,但它却是 Python。

不过也有一些不足,在使用迭代时需要对文本进行重新封装:

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

2. dateutil

安装方法:pip install dateutil

处理日期很痛苦,多亏有了 dateutil

01from dateutil.parser import parse
03>>> parse('Mon, 11 Jul 2011 10:01:56 +0200 (CEST)')
04datetime.datetime(201171110156, tzinfo=tzlocal())
06# fuzzy ignores unknown tokens
08>>> s = """Today is 25 of September of 2003, exactly
09...        at 10:49:41 with timezone -03:00."""
10>>> parse(s, fuzzy=True)
11datetime.datetime(2003925104941,
12                  tzinfo=tzoffset(None-10800))

3. fuzzywuzzy

安装方法:pip install fuzzywuzzy

fuzzywuzzy 可以让你对两个字符串进行模糊比较,当你需要处理一些人类产生的数据时,这非常有用。下面代码使用Levenshtein 距离比较方法来匹配用户输入数组和可能的选择。

01from Levenshtein import distance
03countries = ['Canada''Antarctica''Togo', ...]
05def choose_least_distant(element, choices):
06    'Return the one element of choices that is most similar to element'
07    return min(choices, key=lambda s: distance(element, s))
09user_input = 'canaderp'
10choose_least_distant(user_input, countries)
这已经不错了,但还可以做的更好:
1from fuzzywuzzy import process
3process.extractOne("canaderp", countries)

4. watchdog

安装方法:pip install watchdog

watchdog 是一个用来监控文件系统事件的 Python API和shell实用工具。

5. sh

安装方法:pip install sh

sh 可让你调用任意程序,就好象是一个函数一般:

01from sh import git, ls, wc
03# checkout master branch
06# print(the contents of this directory
09# get the longest line of this file
10longest_line = wc(__file__, "-L")

6. pattern

安装方法:pip install pattern

Pattern 是 Python 的一个 Web 数据挖掘模块。可用于数据挖掘、自然语言处理、机器学习和网络分析。

7. path.py

安装方法:pip install path.py

当我开始学习 Python 时,os.path 是我最不喜欢的 stdlib 的一部分。尽管在一个目录下创建一组文件很简单。

6for in os.listdir(some_dir):
7    files.append(os.path.joinpath(some_dir, f))

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

而有了 path.py ,处理文件路径变得简单:

3some_dir = path('/some_dir')
5files = some_dir.files()
其他的用法:
04>>> path('a/b/c').splitall()
05[path(''), 'a', 'b', 'c']
08>>> path('a'/ 'b' / 'c'
11>>> path('ab/c').relpathto('ab/d/f')
是不是要好很多?