11个你可能不知道的Python库
- 格式:docx
- 大小:1.00 MB
- 文档页数:16
11个你可能不知道的Python库(中文)1 2 3from delorean import Delorean EST ="US/Eastern"d =Delorean(timezone=EST)2) prettytable可以在浏览器或终端构建很不错的输出1 2 3 4 5 6 7 8 9101112131415161718192021 22from prettytable import PrettyTabletable =PrettyTable(["animal", "ferocity"]) table.add_row(["wolverine", 100])table.add_row(["grizzly", 87])table.add_row(["Rabbit of Caerbannog", 110]) table.add_row(["cat", -1])table.add_row(["platypus", 23])table.add_row(["dolphin", 63])table.add_row(["albatross", 44])table.sort_key("ferocity")table.reversesort =True+----------------------+----------+| animal|ferocity |+----------------------+----------+|Rabbit of Caerbannog | 110|| wolverine | 100|| grizzly | 87||dolphin | 63|| albatross | 44|| platypus| 23|| cat | -1|+----------------------+----------+3) snowballstemmer非常瘦小的语言转换库,支持15种语言1 2 3 4 5from snowballstemmer import EnglishStemmer, SpanishStemmer EnglishStemmer().stemWord("Gregory")# GregoriSpanishStemmer().stemWord("amarillo")# amarill4) wgetPython的网络爬虫库1 2 3import wgetwget.download("# 100% [.........................................................................5) PyMCPyMC,一个用于贝叶斯分析的函数库1 2 3 4 5from pymc.examples import disaster_modelfrom pymc import MCMCM =MCMC(disaster_model)M.sample(iter=10000, burn=1000, thin=10)[-----------------100%-----------------] 10000of 10000complete in1.4sec6) sh将shell命令作为函数导入Python脚本1 2 3 4 5 6 7from sh import findfind("/tmp")/tmp/foo/tmp/foo/file1.json/tmp/foo/file2.json/tmp/foo/file3.json/tmp/foo/bar/file3.json7) fuzzywuzzy用于字符串匹配率、令牌匹配等1 2 3from fuzzywuzzy import fuzzfuzz.ratio("Hit me with your best shot", "Hit me with your pet shark") # 858) progressbar如其名,一个滚动条函数库1 from progressbar import ProgressBar2 3 4 5 6 7 8import timepbar =ProgressBar(maxval=10)for i in range(1, 11):pbar.update(i)time.sleep(1)pbar.finish()# 60% |########################################################9) colorama一个色彩库,可以为文本添加丰富的色彩10) uuid一个可以产生唯一uuid的库1 2 3import uuidprint uuid.uuid4()# e7bafa3d-274e-4b0a-b9cc-d898957b4b6111) bashplotlibPython的绘图控件,可以绘制直方图、散点图等1 2$ pip install bashplotlib$ scatter --file data/texas.txt --pch x(原文)There are tons of Python packages out there. So many that no one man or woman could possibly catch them all.PyPi alone has over 47,000 packages listed!Recently, with so many data scientists making the switch to Python, I couldn't help but think that while they're getting some of the greatbenefits of pandas, scikit-learn, and numpy, they're missing out on some older yet equally helpful Python libraries.In this post, I'm going to highlight some lesser-known libraries. Even you experienced Pythonistas should take a look, there might be one or two in there you've never seen!1) deloreanDelorean is a really cool date/time library. Apart from having a sweet name, it's one of the more natural feeling date/time munging libraries I've used in Python. It's sort of like moment in javascript, except I laugh every time I import it. The docs are also good and in addition to being technically helpful, they also make countless Back to theFuture references.2) prettytableThere's a chance you haven't heard of prettytable because it'slisted on GoogleCode, which is basically the coding equivalent of Siberia.Despite being exiled to a cold, snowy and desolateplace, prettytable is great for constructing output that looks good in the terminal or in the browser. So if you're working on a new plug-in for the IPython Notebook, check out prettytable for yourHTML __repr__.3) snowballstemmerOk so the first time I installed snowballstemmer, it was because I thought the name was cool. But it's actually a pretty slick little library. snowballstemmer will stem words in 15 different languages and also comes with a porter stemmer to boot.4) wgetRemember every time you wrote that web crawler for some specific purpose? Turns out somebody built it...and it's called wget. Recursively download a website? Grab every image from a page? Sidestep cookie traces? Done, done, and done.Movie Mark Zuckerberg even says it himselfFirst up is Kirkland, they keep everything open and allow indexes on their apache configuration, so a little wget magic is enough to download the entire Kirkland facebook. Kid stuff!The Python version comes with just about every feature you could ask for and is easy to use.Note that another option for linux and osx users would be to use do: from sh import wget. However the Python wget module does have a better argument handline.5) PyMCI'm not sure how PyMC gets left out of the mix sooften. scikit-learn seems to be everyone's darling (as it should,it's fantastic), but in my opinion, not enough love is given to PyMC.If you don't already know it, PyMC is a library for doing Bayesiananalysis. It's featured heavily in Cam Davidson-Pilon's Bayesian Methods for Hackers and has made cameos on a lot of popular data science/python blogs, but has never received the cult following akin to scikit-learn.6) shI can't risk you leaving this page and not knowing about sh. sh letsyou import shell commands into Python as functions. It's super useful for doing things that are easy in bash but you can't remember how to do in Python (i.e. recursively searching for files).7) fuzzywuzzyRanking in the top 10 of simplest libraries I've ever used (if you have 2-3 minutes, you can read through the source),fuzzywuzzy is a fuzzy string matching library built by the fine people at SeatGeek. fuzzywuzzy implements things like string comparison ratios, tokenratios, and plenty of other matching metrics. It's great for creating feature vectors or matching up records in different databases.8) progressbarYou know those scripts you have where you do a print "still going..." in that giant mess of a for loop you call your __main__? Yeah well instead of doing that, why don't you step up your game and start using progressbar?progressbar does pretty much exactly what you think it does...makesprogress bars. And while this isn't exactly a data science specific activity, it does put a nice touch on those extra long running scripts.Alas, as another GoogleCode outcast, it's not getting much love (the docs have 2 spaces for indents...2!!!). Do what's right and give it a good ole pip install.9) coloramaSo while you're making your logs have nice progress bars, why not also make them colorful! It can actually be helpful for reminding yourself when things are going horribly wrong.colorama is super easy to use. Just pop it into your scripts and add any text you want to print to a color:10) uuidI'm of the mind that there are really only a few tools one needs in programming: hashing, key/value stores, and universally unique ids. uuid is the built in Python UUID library. It implements versions 1, 3, 4, and 5 of the UUID standards and is really handy for doing things like...err...ensuring uniqueness.That might sound silly, but how many times have you had records for a marketing campaign, or an e-mail drop and you want to make sure everyone gets their own promo code or id number?And if you're worried about running out of ids, then fear not! The number of UUIDs you can generate is comparable to the number of atoms in the universe.Well if you were a uuid you probably would be.11) bashplotlibShameless self-promotion here, bashplotlib is one of my creations.It lets you plot histograms and scatterplots using stdin. So while you might not find it replacing ggplot or matplotlib as your everyday plotting library, the novelty value is quite high. At the very least, use it as a way to spruce up your logs a bit.原文:/posts/11-python-libraries-you-might-not-know.html。