python学习4 常用内置模块
logging
os
- 路径处理
// 获取当前路径
os.path.abspath(__file__)
//获取当前文件夹路径
os.path.dirname(os.path.abspath(__file__))
os.path.abspath('.')
//路径拼接处理
os.path.join(path1, path2)
- 创建链接
//创建硬/文件链接
os.link('oops.txt', 'yikes.txt')
//创建符号链接
os.symlink('oops.txt', 'jeepers.txt')
//检查文件还是符号链接
os.path.islink('jeepers.txt') //False
//获取符号链接路径
os.path.realpath('jeepers.txt')
- 获取进程信息
import os
os.getegid()
os.getcwd()
shutil
- 复制内容
import shutil
shutil.copy('oops.txt', 'ohno.txt')
glob
- 列出匹配文件
import glob
print(glob.glob('*'))
sys
- 获取执行参数
sys.argv
subprocess
- 执行命令
subprocess.Popen(command)
time
- 使用
sleep
try:
while True:
print('start')
time.sleep(2)
print('end')
except KeyboardInterrupt:
print('stop')
- 事件处理
//获取当前秒
time.time()
datetime
- 通过时间戳获取日期
from datetime import datetime
dt = datetime.fromtimestamp(t)
dt.year ;dt.month ;dt.day
inspect 检查运行模块的一些基本信息
- 判断generator函数
from inspect import isgeneratorfunction
isgeneratorfunction(fab)
- 获取参数
inspect.signature(fn)
types
- 判断generator函数和generator实例
import types
isinstance(fab, types.GeneratorType)
isinstance(fab(5), types.GeneratorType)
pickle
- 把对象序列化成bytes
//把对象序列化成bytes
byte_data = pickle.dumps({"name": "jinks"})
//反操作
pick.loads(byte_data)
json
- 例子
- Python对象和JSON的转化
json_str = json.dumps(data)
data = json.loads(json_str)
functools 高阶函数相关的模块
- 消除装饰器带来__name__改变的副作用
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@decorator
def add(x, y):
return x + y
urllib, urllib2 处理url相关操作的库
- 分析http查询字符串
urllib.parse.parse_qs 返回字典
urllib.parse.parse_qsl 返回列表
collections 内建的集合模块
- deque实现插入删除操作的双向列表
from collections import deque
q = deque(['a', 'b', 'c'])
q.append('x')
q.appendleft('y')
heapq 实现堆排序
- 查找最值
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]
//
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
re
- split 字符串分割
from re import split
line = 'asdf fjdk; afed, fjek,asdf, foo'
rs = split(r'[;,\s]\s*', line)
python学习4 常用内置模块的更多相关文章
- Python学习 :常用模块(二)
常用模块(二) 四.os模块 os模块是与操作系统交互的一个接口,用于对操作系统进行调用 os.getcwd() # 提供当前工作目录 os.chdir() # 改变当前工作目录 os.curdir( ...
- Python 学习:常用函数整理
整理Python中常用的函数 一,把字符串形式的list转换为list 使用ast模块中的literal_eval函数来实现,把字符串形式的list转换为Python的基础类型list from as ...
- Python学习路程-常用设计模式学习
本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...
- Python学习笔记-常用模块
1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...
- Python学习 :常用模块(三)----- 日志记录
常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...
- Python学习笔记——常用的内置函数
一.yield def EricReadlines(): seek = 0 while True: with open('D:/temp.txt','r') as f: f.seek(seek) da ...
- python 学习分享-常用模块篇
模块 就是前人给你造的轮子,你开车就好!!! 常用模块有: time模块 random模块 os模块 sys模块 shutil模块 json & picle模块 shelve模块 xml处 ...
- Python学习-day5 常用模块
day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...
- 05 python学习笔记-常用内置函数(五)
1.sorted() 函数对所有可迭代的对象进行排序(默认升序)操作 sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. l ...
随机推荐
- 用swing也可以做出好看的界面
用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...
- 关于Block的简单使用
Block在整个iOS开发中无所不见,很重要,很重要,文本在这里block的简单使用介绍.我们可以简单地定义.使用block. 1. Block和C的指针函数很像,但比C的函数灵活多了.废话了.... ...
- 常用linux 命令 -字符串相关
参考网络文章,个人工作总结 题记:一般对字符串的操作有以下几种:求长度,截取字符串,拼接字符串,找字符串中某个字符的索引 1 expr 命令 1.1 定义 man 手册 Print the value ...
- Maven POM元素继承
为了减少重复代码的编写,我们需要创建POM的父子结构,然后在POM中申明一些配置供子POM继承,以实现"一处申明,多处使用的"目的.以之前的模块中的结构为基础,在account-a ...
- windows安装redis
下载安装包,由于redis不提供windows版本,但是通过官网了解,如下: The Redis project does not officially support Windows. Howeve ...
- jvm--1.class文件结构
1.字节码(1)bytecode是构成平台无关性的基石 (2)当jvm发展到1.7-1.8的时候,jvm设计者通过,JSR-292,基本可以让其他语言运行在jvm上面. 如,Clojure , Gro ...
- 配置子目录Web.config使其消除继承,用虚拟目录创建多个网站的方法
来源:http://www.wtnzone.com/post/2011/02/20/Set-Web-Config-to-Turn-Inheritance-Off.aspx ASP.NET提供了强大的W ...
- java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)
学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...
- Android学习资料收集
1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/
- redis实战(01)_redis安装
早就想对redis进行实战操作了,最近看了一些视频和参考书籍,总结总结一下,redis实战内容: 实战前先对redis做一个大概的认识: 现在开始安装redis了... redis的安装下载地址 ht ...