python学习之day6,常用标准模块
1.时间模块 time
import time
#时间戳转字符串格式
a = time.time()
print(a) #打印时间戳
b = time.localtime(a) #把时间戳转换成时间对象 元组的形式
print(b)
c = time.strftime("%Y-%m-%d %H:%M:%S",b) #格式化时间 把事件对象转化成格式化的字符串
print(c)
#字符串时间转化为时间戳
d = time.strptime("2016-11-14 09:37:26","%Y-%m-%d %H:%M:%S")
print(d)
e = time.mktime(d)
print(e)
#时间加减
import datetime
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
2.random模块 生成随机字符
import random
import string
print( random.randint(1,2)) #包含1和2
print(random.randrange(1,3)) #1和2 会出现,3不会出现
#随机生成验证码或密码
str_source = string.ascii_letters + string.digits
suji = random.sample(str_source,8)
print(suji)
print("".join(suji))
3.shutil 模块 复制,删除,打包压缩
import shutil
#shutil.copy("time模块.py","a") #copy一个文件到另一个文件,包括文件内容和权限
#shutil.copytree(r"C:\Users\Administrator\Documents\Tencent Files\363572453\FileRecv\a","C:\liruixin") #递归的复制目录
#shutil.rmtree("C:\liruixin") #递归的删除目录
#shutil.move() #递归的移动目录
#tmp = shutil.make_archive("C:\svntest","zip",root_dir=r"D:\testsvn") #压缩文件 root_dir为原文件 C盘svntest为压缩后的路径及压缩后的文件名
4.hashlib 模块 可以用来校验文件一致性
import hashlib a = hashlib.md5() #生成一个对象
#a.update(b"abc") #加密
#print(a.hexdigest()) #打印MD5值 #校验一个文件的MD5值
f = open("a")
for i in f:
a.update(i.encode())
print(a.hexdigest())
f.close()
5. logging 模块
import logging
from logging import handlers
#日志输出到屏幕
# logging.warning("waring message")
# logging.critical("server is down")
# logging.error("have error")
# logging.debug("print message")
# logging.info("info")
# #最简单的把日志打印到文件
# logging.basicConfig(filename="test.log",
# format='%(asctime)s %(levelname)s: %(filename)s %(lineno)d %(message)s',
# datefmt='%m/%d/%Y %I:%M:%S',
# level=logging.INFO)
# logging.debug("debug")
# logging.info("info")
# logging.warning("waring")
# logging.error("error")
# logging.critical("critical") #日志多输出
# #create logger
# logger = logging.getLogger("test.log")
# logger.setLevel(logging.DEBUG)
# #create console handler and set level to debug
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# #create file handler and set level to waring
# fh = logging.FileHandler("out.log",encoding="utf-8")
# fh.setLevel(logging.WARNING)
#日志格式
# fh_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)d - %(levelname)s: %(message)s')
# ch_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)d - %(levelname)s: %(message)s')
# #把日志格式加到handler中
# fh.setFormatter(fh_formatter)
# ch.setFormatter(ch_formatter)
#写明要输出的地方
# logger.addHandler(fh)
# logger.addHandler(ch)
#执行打印日志
# logger.debug("I am debug")
# logger.error("I am error") ###日志文件自动截断####
#测试按时间截断
# logger = logging.getLogger("test")
# log_file = "time_log.log"
# fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
# fh = handlers.TimedRotatingFileHandler(filename=log_file,when="s",interval=3,backupCount=2,encoding="utf-8") #按时间截断
# formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
# fh.setFormatter(formatter)
# logger.addHandler(fh)
# import time
# logger.error("error1")
# time.sleep(2)
# logger.error("error2")
# time.sleep(6)
# logger.error("error3") # #测试按文件大小截断
# logger = logging.getLogger("test")
# log_file = "data_log.log"
# fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
# formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
# fh.setFormatter(formatter)
# logger.addHandler(fh)
# logger.error("error1,error2,error3,error4,")
# logger.warning("waring") ###详细请参考博客 http://www.cnblogs.com/alex3714/articles/5161349.html
python学习之day6,常用标准模块的更多相关文章
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习笔记之常用函数及说明
Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...
- Python学习系列(六)(模块)
Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...
- Python学习day17-常用的一些模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python学习第四十八天json模块与pickle模块差异
在开发过程中,字符串和python数据类型进行转换,下面比较python学习第四十八天json模块与pickle模块差异. json 的优点和缺点 优点 跨语言,体积小 缺点 只能支持 int st ...
- python开发学习-day05(正则深入、冒泡排序算法、自定义模块、常用标准模块)
s12-20160130-day05 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- [Python] Python学习笔记之常用模块总结[持续更新...]
作为一种极其简单的编程语言,Python目前成为了最炙手可热的几种语言之一.它不仅简单易学,而且它还为用户提供了各种各样的模块,功能强大,无所不能.有利必有弊,学习Python同样有困扰,其中之一就是 ...
- python学习日记(常用模块)
模块概念 什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代 ...
- python学习之算法、自定义模块、系统标准模块(上)
算法.自定义模块.系统标准模块(time .datetime .random .OS .sys .hashlib .json和pickle) 一:算法回顾: 冒泡算法,也叫冒泡排序,其特点如下: 1. ...
- Python 学习笔记(6)--常用模块(2)
一.下载安装 下载安装有两种方式: yum\pip\apt-get 或者源码 下载源码 解压源码 进入目录 编译源码 python setup.py build 安装源码 python setup.p ...
随机推荐
- WPF 后台数据触发改变界面状态-心跳实现
今年做的一个上位机工控WPF项目,做个小小的总结把,以后随时来找 请不要带血乱喷,我只是菜鸟.___by 鲍队 类似于这样子的;大致的意思是:一个代码变量,通过改变变量的值,绑定这个变量的这个圆颜色也 ...
- 查看Sql Server被锁的表以及解锁
查看被锁表: select spId from master..SysProcesses where db_Name(dbID) = '数据库名称' and spId <> @@SpId ...
- C#开发微信门户及应用(40)--使用微信JSAPI实现微信支付功能
在我前面的几篇博客,有介绍了微信支付.微信红包.企业付款等各种和支付相关的操作,不过上面都是基于微信普通API的封装,本篇随笔继续微信支付这一主题,继续介绍基于微信网页JSAPI的方式发起的微信支付功 ...
- 在Word中输入数学公式
office官方说明:https://support.office.com/en-us/article/Linear-format-equations-and-Math-AutoCorrect-in- ...
- Python 操作 MS Excel 文件
利用 Python 对 Excel 文件进行操作需要使用第三方库: openpyxl,可执行 pip install openpyxl 进行安装 1. 导入 openpyxl 模块 导入 openpy ...
- 深入理解 RESTful Api 架构
转自https://mengkang.net/620.html 一些常见的误解 不要以为 RESTful Api 就是设计得像便于 SEO 的伪静态,例如一个 Api 的 URL 类似于 http: ...
- jQuery实现DOM加载方法源码分析
传统的判断dom加载的方法 使用 dom0级 onload事件来进行触发所有浏览器都支持在最初是很流行的写法 我们都熟悉这种写法: window.onload=function(){ ... } 但 ...
- 浅谈Android编码规范及命名规范
前言: 目前工作负责两个医疗APP项目的开发,同时使用LeanCloud进行云端配合开发,完全单挑. 现大框架已经完成,正在进行细节模块上的开发 抽空总结一下Android项目的开发规范:1.编码规范 ...
- ASP.NET MVC 3 网站优化总结(三)Specify Vary: Accept-Encoding header
继续进行 ASP.NET MVC 3 网站优化工作,使用 Google Page 检测发现提示 You should Specify Vary: Accept-Encoding header,The ...
- python from __future__ import division
1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...