1.时间模块

import time,datetime

# print(time.time())                   #时间戳
# print(time.strftime("%Y-%m-%d %X")) #格式化时间
# print(time.localtime()) #本地的struct_time
# print(time.gmtime()) #本地的utc时间
# print(time.ctime()) #英文日期 #时间加减
print(datetime.datetime.now()) #显示当前时间的毫秒
print(datetime.date.fromtimestamp(time.time())) #将时间戳直接转成日期格式
print(datetime.datetime.now() + datetime.timedelta(3)) #显示现在时间加三天
print(datetime.datetime.now() + datetime.timedelta(-3)) #显示现在时间减三天
print(datetime.date.fromtimestamp(time.time()) + datetime.timedelta(3)) #当前时间取整加三
print(datetime.date.fromtimestamp(time.time()) + datetime.timedelta(-3)) #当前时间取整减三
print(datetime.datetime.now() + datetime.timedelta(hours=3))#显示现在时间加三小时
print(datetime.datetime.now() - datetime.timedelta(hours=3))#显示现在时间减三小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30))#显示现在时间加三十分
#时间替换
now_time = datetime.datetime.now()
print(now_time.replace(minute=3,hour=2))
  • 其中计算机认识的时间只能是'时间戳'格式,而程序员可处理的或者说人类能看懂的时间有: '格式化的时间字符串','结构化的时间' ,于是有了下图的转换关系

2.random模块

import random

# print(random.random())     #随机生成一个大于0到小于1的小数       (0,1)
# print(random.randint(1,3)) #随机生成一个大于等于1小于等于三的整数 [1,3] #打乱列表顺序
# item=[1,2,3,4,5,6]
# random.shuffle(item)
# print(item) #生成几位随机验证码
def range_code(n):
res=''
for i in range(n):
s1=chr(random.randint(65,90))
s2=str(random.randint(0,9))
res+=random.choice([s1,s2])
return res print(range_code(6))

3.logging模块

'''
日志级别:
critical(严重)=50
error(错误)=40
warning(警告)=30
info(正常)=20
debug(调试级别)=10
notset(没有设置日志级别)=0
默认用root产生日志
控制日志打印到文件中,并且自己定制日志的输出格式
''''' # import logging #默认是warning
#
# logging.basicConfig(
# filename="access.log",
# format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
# datefmt="%Y-%m-%d %H:%M:%S",
# level=10,
# )
# logging.debug("debug:debug log")
# logging.info("info:info log")
# logging.warning("warning: warning log")
# logging.error("error: error log")
# logging.critical("critical: critical log") import logging #产生对象
logger=logging.getLogger("root")
#产生日志
# logger.debug("debug")
# logger.info("info:info log")
# logger.warning("warning: warning log")
# logger.error("error: error log")
# logger.critical("critical: critical log")
#filter对象,过滤,省略
#handler对象,负责接收logger对象传来的日志内容,控制打印到哪里,
h1=logging.FileHandler("t1.log")
h2=logging.FileHandler("t2.log")
h3=logging.StreamHandler()
#定制日志格式
#给文件
formatter1=logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt="%Y-%m-%d %H:%M:%S",
)
#给终端
formatter2=logging.Formatter(
'%(asctime)s - %(message)s',
datefmt="%Y-%m-%d %H:%M:%S",
) #建立关系:logger对象才能把自己的日志交给handle负责输出
logger.addHandler(h1)
logger.addHandler(h2)
logger.addHandler(h3)
logger.setLevel(10) #绑定日志格式到Filehandler(文件)对象
h1.setFormatter(formatter1)
h2.setFormatter(formatter1)
#绑定日志格式到StreamHandler(终端)对象
h3.setFormatter(formatter2) #设置日志级别
h1.setLevel(10)
h2.setLevel(10)
h3.setLevel(30) logger.debug("debug")
logger.info("info")
logger.warning("warning")
# logger.error("error")
# logger.critical("critical")

4.re模块

4.1 匹配模式

import re
# print(re.findall('asd',"asd asd ads asdasd 1#¥%das")) #匹配到了4个
# print(re.findall('aaa',"aaaa")) #只匹配到1个
'''
\w 匹配字数数字下滑线
\W 不匹配字母数字下滑线
'''
# print(re.findall("\w","hello world 123_456 $%"))
# print(re.findall("\W","hello world 123_456 $%"))
# print(re.findall("\s","hello world 123_456 $%"))
# print(re.findall("\S","hello world 123_456 $%"))
# print(re.findall("\d\d","hello world 123_456 $%"))
# print(re.findall("^h","hello world 123_456 $%"))
# print(re.findall("\$$","hello world 123_456 $%$"))
# print(re.findall("a.c","abc asc a&c a\nc a c"))
# print(re.findall("a.c","abc asc a&c a\nc a c",re.S)) #re.S可以匹配到换行 '''
. 任意
* 出现0次以上
+ 至少出现一次
? 出现0次或1次
.* 贪婪匹配
.*? 非贪婪匹配
'''

5. json模块

import json
user={"name":"xiaojin","pwd":123}
with open("db.txt","w",encoding="utf-8") as write_f:
line=json.dumps(user)
write_f.write(line) with open("db.txt","r",encoding="utf-8") as read_f:
data=read_f.read()
dic=json.loads(data)
print(dic["name"])

  

Python常用模块系列的更多相关文章

  1. Python 常用模块系列(2)--time module and datatime module

    import time print (help(time)) #time帮助文档 1. time模块--三种时间表现形式: 1° 时间戳--如:time.time()  #从python创立以来,到当 ...

  2. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  3. python常用模块集合

    python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...

  4. Python常用模块之sys

    Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 常见用法 sys.argv 可以用sys.argv获取当前正在执行的命令行参数的参数列表(list). 变量 ...

  5. Python常用模块中常用内置函数的具体介绍

    Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...

  6. python——常用模块2

    python--常用模块2 1 logging模块 1.1 函数式简单配置 import logging logging.debug("debug message") loggin ...

  7. python——常用模块

    python--常用模块 1 什么是模块: 模块就是py文件 2 import time #导入时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的 ...

  8. Python常用模块——目录

    Python常用模块学习 Python模块和包 Python常用模块time & datetime &random 模块 Python常用模块os & sys & sh ...

  9. python 常用模块之random,os,sys 模块

    python 常用模块random,os,sys 模块 python全栈开发OS模块,Random模块,sys模块 OS模块 os模块是与操作系统交互的一个接口,常见的函数以及用法见一下代码: #OS ...

随机推荐

  1. QTP read or write XML file

    'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...

  2. luoguP1525 关押罪犯 题解(NOIP2010)(并查集反集)

    P1525 关押罪犯  题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<c ...

  3. Python - zipfile 乱码问题解决

    最近使用zipfile进行解包过程中遇到了很不舒服的问题,解包之后文件名是乱码的.下面进行简单总结: 首先,乱码肯定是因为解码方式不一样了,zipfile使用的是utf-8和cp437这两种编码方式, ...

  4. 【转】modulenotfounderror: no module named 'matplotlib._path'问题的解决

    今天在装matplotlib包的时候遇到这样的问题,在网上找了很长时间没有类似很好的解决方法,最后自己 研究找到了解决的方法. 之前在pycharm里面已经装了matplotlib包,之后觉着下载包挺 ...

  5. C#设计模式:状态者模式(State Pattern)

    一,什么是状态设计模式? 1,定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 2,当一个对象的内部状态改变时允许改变其行为,这个对象看起来像是 ...

  6. MAS(转)

    1.为什么要使用微服务? 要说为什么要使用微服务,我们要先说下传统的企业架构模式-垂直架构/单块架构模式,简单点说:我们一般将系统分为三层架构,但是这是逻辑上的三层,而非物理上的三层,这就意味着经过编 ...

  7. 【leetcode】901. Online Stock Span

    题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...

  8. vue 路由动态传参 (多个)

    动态传参 传值页面  客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面  客户详情CustomerDetails.vue 通过this.$router.para ...

  9. linux centos7 安装Phabircator

    Phabricator 是facebook开发的一套代码审核工具,基于PHP和Mysql开发. 准备工作: 系统:Linux CentOS7 环境: Apache(或nginx,或lighttpd): ...

  10. C常量

    C 常量 常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量. 常量可以是任何的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量. 常量就像是常规的变量,只不 ...