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 ...
随机推荐
- shell 计算2
转载 http://www.th7.cn/system/lin/201309/44683.shtml expr bc 在Linux下做算术运算时你是如何进行的呢?是不是还在用expr呢?你会说我还会b ...
- 【bzoj4721】[Noip2016]蚯蚓
题目描述 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓.蛐 ...
- Bootstrap table使用心得
序号显示带分页信息的连续编号,在序号列添加以下格式化代码即可. { field: 'number', title: '序号', align:'center', width:45, formatter: ...
- C和指针 第十六章 标准函数库
字符串转换: long int strtol(char const *string, char **unused, int base); 将字符串转换为数值形式,遇到非法字符停止,如果stop不是NU ...
- Thinkphp 第二篇:如何将一个外部项目导入到Thimkphp环境中
一:到这篇博文的开头,假设我们的Xmapp环境已经搭建好了,并且成功的安装了Thinkphp的环境了. 1:Xmapp安装成功截图: 2:Thinkphp安装成功截图: 二:Thinkphp中各个文件 ...
- iOS9,导航控制器中的子控制器设置StatusBar状态失效的问题
iOS9之前控制StatusBar的两种方式: 第一种方式:全局控制StatusBar 1. 在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppe ...
- ubuntu专用
独立显卡处理驱动处理问题: http://blog.csdn.net/liufunan/article/details/52090382 git的教程: http://www.bootcss.com/ ...
- DevExpress GridControl 选择整行被选单元格不变色的设置
设置GridControl 里面的 gridview 属性, 找到OptionSelection 将EnableAppearanceFocusedCell 属性设置False 就可以了 此方式同样适用 ...
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- Atom安装以及activate-power-mode atom package插件安装
1.首先安装node.js,不然没有npm命令可用. 现在很多开源程序都使用npm来管理依赖包,所以node.js必备呀. 2.去下载atom安装包和activate-power-mode-0.5.2 ...