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 ...
随机推荐
- .NET正则表达式基础入门
这是我第一次写的博客,个人觉得十分不容易.以前看别人写的博客文字十分流畅,到自己来写却发现十分困难,还是感谢那些为技术而奉献自己力量的人吧. 本教程编写之前,博主阅读了<正则指引>这本入门 ...
- angular-JS模仿Form表单提交
直接上示例代码,有不懂的欢迎留言: $http({ url: "http://localhost:10086/yuanxin/Conference/ImportExcelDataForBus ...
- 【C#进阶系列】27 I/O限制的异步操作
上一章讲到了用线程池,任务,并行类的函数,PLINQ等各种方式进行基于线程池的计算限制异步操作. 而本章讲的是如何异步执行I/O限制操作,允许将任务交给硬件设备来处理,期间完全不占用线程和CPU资源. ...
- 【Java每日一题】20161227
package Dec2016; public class Ques1227 { public static void main(String[] args){ } { c = 1; } int c ...
- 【问题】Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数
[问题]Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数. [解决]直接对变量加引号,如: <button onclick="deleteProduct('@ ...
- 自己实现一个简易web服务器
一个web服务器是网络应用中最基础的环节. 构建需要理解三个内容: 1.http协议 2.socket类 3.服务端实现原理 1.1 HTTP http请求 一般一个http请求包括以下三个部分: 1 ...
- ios native工程集成react-native的demo
react-native看到了给现有工程添加react-native环境的时候碰到一个问题: 如何往工程中添加 package.json文件,以及node_modules是怎么来的? 我开始的时候以为 ...
- SQL Server快速查询某张表的当前行数
传统做法可能是select count(1) 但是往往会比较慢.推荐如下做法: ) CurrentRowCount FROM sys.sysindexes WHERE id = OBJECT_ID(' ...
- ASP.NET MVC 3 网站优化总结(三)Specify Vary: Accept-Encoding header
继续进行 ASP.NET MVC 3 网站优化工作,使用 Google Page 检测发现提示 You should Specify Vary: Accept-Encoding header,The ...
- CentOS中的环境变量配置文件
CentOS的环境变量配置文件体系是一个层级体系,这与其他多用户应用系统配置文件是类似的,有全局的,有用户的,有shell的,另外不同层级有时类似继承关系.下面以PATH变量为例. 1.修改/etc/ ...