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 ...
随机推荐
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- java基础知识 多线程
package org.base.practise9; import org.junit.Test; import java.awt.event.WindowAdapter; import java. ...
- redis incr incrby decr decrby命令
incr.incrby.decr.decrby命令的作用和用法 redis中incr.incrby.decr.decrby属于string数据结构,它们是原子性递增或递减操作. incr递增1并返回递 ...
- 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />
在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...
- HttpClient调用webApi时注意的小问题
HttpClient client = new HttpClient(); client.BaseAddress = new Uri(thisUrl); client.GetAsync("a ...
- H5 本地存储一
localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...
- css权威指南--笔记
第1章 css和文档 1,元素:替换元素(img input),非替换元素(大多数span). 2,link:rel(代表关系:stylesheet,候选样式表:alternate styleshee ...
- 在VMware上安装Linux(CentOS)
1. 新建虚拟机 2. 新建虚拟机向导 3. 创建虚拟空白光盘 4. 安装Linux系统对应的CentOS版 5. 虚拟机命名和定位磁盘位置 6. 处理器配置,看自己是否是双核.多核 7. 设置内存为 ...
- Linux Shell脚本逻辑操作符简介
在写程序时,会用到条件判断,测试条件是否成立.很多时候,判断条件是多个的,这个时候需要用到逻辑操作符.shell脚本中常用的有哪些逻辑操作符呢? 1.逻辑与: -a 格式: conditon1 -a ...
- Tomcat 服务应用
转自:http://wiki.jikexueyuan.com/project/tomcat/windows-service.html Tomcat8 是一个服务应用,能使 Tomcat 8 以 Win ...