← 返回首页

python常用库

📖 正文内容

一、系统自带常用包:

datetime    为日期和时间处理同时提供了简单和复杂的方法

from datetime import date #导入时间库 now=date.today() #取当前时间 print(now) birthday=date(1987,12,3) print(birthday) age=now-birthday #假设年龄=当前日期-生日日期 print(age)

zlib    直接支持通用的数据打包和压缩格式:zlib,gzip,bz2,zipfile,以及 tarfile

import zlib m = b'This is a test compress' print(m) m1=len(m) #查看字符串的长度 print(m1) t = zlib.compress(m) #假设压缩后的内容为t t1=len(t) #查看压缩后内容t的长度 print(t) print(t1) s = zlib.decompress(t) #解压缩后的内容为s print(s)

random    提供了生成随机数的工具

math    为浮点运算提供了对底层C函数库的访问

sys    工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量

import sys a=sys.path #假设系统路径为a print(a)

glob    提供了一个函数用于从目录通配符搜索中生成文件列表

os    提供了不少与操作系统相关联的函数

urllib    获取网页源码   不常用

# coding=UTF-8 import urllib url = 'https://blog.csdn.net/alice_tl' wp = urllib.urlopen(url) file_content = wp.read() print file_content #第一部分为获取网页源码 fp = open('alice.txt', 'wb') #打开一个文本文件 fp.write(file_content) #写入数据 fp.close() #关闭文件 #第二部分为将网页内容存入文件中 #第三部分为利用正则表达式将文件内容打印出来 import re fp = open('alice.txt', 'rb') content = fp.read() fp.close() title = re.search('<title>(.*?)</title>', content, re.S).group(1) print 'title = ', title + '\n' hrefPatten = 'href="(.*?)"' hrefC = re.findall(hrefPatten, content, re.S)  #返回所有匹配正则表达式的值于列表中 print 'Allhref = ', hrefC for h in hrefC :     print h

二、外部常用包:

Scrapy    爬虫工具常用的库

Requests    http库    python做接口测试或者爬数据常用                       推荐用这个

requests-html    和正则一样解析爬取的数据 from requests_html import HTMLSession

BeautifulSoup    xml和html的解析库,对于新手非常有用

lxml    爬虫时候用来处理源代码的库

openpyxl    用于处理excel文件的库

Pillow    是PIL(Python图形库)的一个分支。适用于在图形领域工作的人    Python实现图像处理:PiL依赖库的应用_简言-CSDN博客

matplotlib    绘制数据图的库。对于数据科学家或分析师非常有用    

OpenCV    图片识别常用的库,通常在练习人脸识别时会用到    

pytesseract    图片文字识别,即OCR识别    

jira    操作jira,查询Jira信息,

wxPython    Python的一个GUI(图形用户界面)工具    

Twisted    对于网络应用开发者最重要的工具    

SymPy    SymPy可以做代数评测、差异化、扩展、复数等等    

SQLAlchemy    数据库的库    

SciPy    Python的算法和数学工具库    

Scapy    数据包探测和分析库    

pywin32    提供和windows交互的方法和类的Python库    

pyQT    Python的GUI工具。给Python脚本开发用户界面时次于wxPython的选择

pyGtk    也是Python GUI库    

Pyglet    3D动画和游戏开发引擎    

Pygame    开发2D游戏的时候使用会有很好的效果    

NumPy    为Python提供了很多高级的数学方法    

nose    Python的测试框架    

nltk    自然语言工具包    

IPython    Python的提示信息。包括完成信息、历史信息、shell功能,以及其他很多很多方面    



💡 示例代码

<div style='--en-codeblock:true;--en-blockId:xTMsTumJ7jQ;--en-meta:{"title":"datetime引用方法","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>from datetime import date
#导入时间库
now=date.today()
#取当前时间
print(now)
birthday=date(1987,12,3)
print(birthday)
age=now-birthday
#假设年龄=当前日期-生日日期
print(age)</div>

<div style='--en-codeblock:true;--en-blockId:LhTQQCXshz9;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>import zlib
m = b'This is a test compress'
print(m)
m1=len(m)
#查看字符串的长度
print(m1)
t = zlib.compress(m)
#假设压缩后的内容为t
t1=len(t)
#查看压缩后内容t的长度
print(t)
print(t1)
s = zlib.decompress(t)
#解压缩后的内容为s
print(s)</div>

<div style='--en-codeblock:true;--en-blockId:OnD6SejKc-K;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>import sys
a=sys.path
#假设系统路径为a
print(a)</div>

<div style='--en-codeblock:true;--en-blockId:wX2cR-NeJn_;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># coding=UTF-8
 
import urllib
url = 'https://blog.csdn.net/alice_tl'
wp = urllib.urlopen(url)
file_content = wp.read()
 
print file_content
#第一部分为获取网页源码
 
fp = open('alice.txt', 'wb') #打开一个文本文件
fp.write(file_content) #写入数据
fp.close() #关闭文件
#第二部分为将网页内容存入文件中
 
#第三部分为利用正则表达式将文件内容打印出来
import re
 
fp = open('alice.txt', 'rb')
content = fp.read()
fp.close()
 
title = re.search('&lt;title&gt;(.*?)&lt;/title&gt;', content, re.S).group(1)
 
print 'title = ', title + '\n'
 
hrefPatten = 'href="(.*?)"'
hrefC = re.findall(hrefPatten, content, re.S)  #返回所有匹配正则表达式的值于列表中
 
print 'Allhref = ', hrefC
 
for h in hrefC :
    print h</div>

💭 技巧提示

💡

📚 参考资料

💬 评论交流

👤
用户 A 2026-03-25

这个技巧很实用,感谢分享!