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 ...
随机推荐
- [Tool] 插入折叠区域功能
之前写了一个 仿博客园网页端推荐的插入代码插件, 后来在总结一些技术文档时,总是想把一些属性或者方法,参数等,都用表格的形式清晰的列举出来,但是插入的表格太大的话,上下跨度就显得特别大,来回上下滚动的 ...
- MVC 传值
1.ViewBag Controller:ViewBag.Message = "Hello, Word"; View:@ViewBag.Message 注:View ...
- ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...
- 浏览器对localstorage的支持情况以及localstorage在saas系统中的应用实践思考
首先,还是要说,任何一种新特性的引入,通常有着其特有的场景和解决的目标需求,localstorage也一样.在我们的应用场景中,主要在金融业务服务的saas系统.其中涉及很多更改频率很多的元数据的客户 ...
- Linux常用命令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIO ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
- 贝塞尔曲线(UIBezierPath)属性、方法汇总
UIBezierPath主要用来绘制矢量图形,它是基于Core Graphics对CGPathRef数据类型和path绘图属性的一个封装,所以是需要图形上下文的(CGContextRef),所以一般U ...
- IOS 杂笔-12(类别de巧用 有便于Frame的操作)
在实际开发中很多时候我们都为了控件frame的操作焦头烂额. 例如:我们只想要获取view的width. 我们可以这么操作:view.frame.size.width 有时我们想要改变view的wid ...
- iOS字体加载三种方式
静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...
- Mongodb 3.2 Manual阅读笔记:CH9 存储
9. 存储 9. 存储 9.1 存储引擎 9.1.1 WiredTiger存储引擎 9.1.1.1 文档级别并发 9.1.1.2 快照和检查点 9.1.1.3 Journaling 9.1.1.4 压 ...