您当前的位置: 首页 >  Python

壹小俊

暂无认证

  • 2浏览

    0关注

    885博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第四课--python模块json,hashlib,base64,datetime,logging

壹小俊 发布时间:2019-07-27 18:45:26 ,浏览量:2

json是一种数据格式,也是通用的数据格式,他不是数据结构,是字符串。
python3中内置了json包也就是直接引用就可以,import json
看案例
import json
# my_dic = {
#     'a':1,
#     'b':[1,2,3],
#     'c':True,
#     'd':None,
#     'e':(1,2,3,4)
# }
# res = json.dumps(my_dic,ensure_ascii=False)#编码json
# print(res)
# res = json.loads(res)#解码json
# print(res)
# with open('./res.json','w+') as file:
#     res = json.dump(my_dic,fp=file,ensure_ascii=False)#编码json写入文件
#     print(res)
# with open('./res.json','r+') as file:#读取文件中的json
#     res = json.load(fp=file)
#     print(res)
data = {
"name": "nanbei",
"age": 18,
"feature" : [" 高 ", " 富 ", " 帅 "]
 }
with open('./test.json','w+') as file:
    json.dump(data,fp=file,ensure_ascii=False)
with open('./test.json','r+') as file:
    res = json.load(file)
    print(res['feature'][2:])
哈希是一种加密格式,也是python中的一个包。
import hashlib
res = hashlib.new('md5','str'.encode())字符串必须是二进制
print(res.digest())输出是二进制
print(res.hexdigest())输出是字符串
看案例
import hashlib
res = hashlib.new('md5','str'.encode())
print(res.digest())
print(res.hexdigest())

res = hashlib.md5('str'.encode())
print(res.digest())
print(res.hexdigest())

res = hashlib.sha1('str'.encode())
print(res.digest())
print(res.hexdigest())

res = hashlib.md5('st'.encode())
res.update('r'.encode())
print(res.hexdigest())
base64是一种可逆向的加密格式。
import base64
b = 'str'.encode()#字符串编码
res = base64.b64encode(b)
print(res)
res = base64.b64decode(res)
print(res.decode())
看案例
import base64
b = 'str'.encode()#字符串编码
res = base64.b64encode(b)
print(res)
res = base64.b64decode(res)
print(res.decode())
url = 'www.baidu.com'.encode()#url编码
res = base64.urlsafe_b64encode(url)
print(res)
res = base64.urlsafe_b64decode(res)
print(res.decode())
datetime是python中对时间的一种操纵包。
看案例
# import time
# print('我是谁')
# time.sleep(3)#睡眠
# print('hwj')
# print(time.time())
import datetime
date1 = datetime.datetime.now()#当前时间
print(date1)
date3 = datetime.date(2019,1,7)#格式化时间
print(date3)
from datetime import datetime
date2 = datetime.now()#当前时间
print(date2.timestamp())
print(date2)
logging是python中写日志的包。
看案例
import logging
'''
日志的记录
'''
# filename = './test_log.txt'
# level = logging.WARNING
# logging.basicConfig(filename=filename,format='%(asctime)s--%(levelname)s--%(message)s',level=level)
# logging.debug('I am debug')
# logging.info('I am info')
# logging.warning('I am warning')
# logging.error('I am error')
# logging.critical('I am CRI')
# with open(filename,'r') as file:
#     print(file.read())


'''
记录器
'''
filename = './test_log.txt'
logger = logging.getLogger(filename)#实例化
logger.setLevel(logging.DEBUG)#设置一个级别
# 定义一个管理者(handler)
sh = logging.StreamHandler()#控制台输出
sh.setLevel(logging.DEBUG)
# 在文件中输出
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)

formatter = logging.Formatter(
    '时间:%(asctime)s'
    '日志消息:%(message)s'
    '级别消息:%(levelname)s'
)

sh.setFormatter(formatter)#在控制台上格式化
fh.setFormatter(formatter)#在文件中格式化
logger.addHandler(sh)#添加处理器对象
logger.addHandler(fh)#添加处理器对象

if __name__ == '__main__':
    # logger.debug('I am debug')
    # logger.info('I am info')
    # logger.warning('I am warning')
    # logger.error('I am error')
    # logger.critical('I am CRI')

    # with open(filename,'r') as file:
    #     print(file.read())

    def fun(a):
        try:
            num = 40 / a
            logger.debug(num)
        except Exception as e:
            logger.error(e)
    fun(0)
    with open(filename,'r') as file:
        print(file.read())

 

关注
打赏
1664335782
查看更多评论
立即登录/注册

微信扫码登录

0.3764s