python第十五节(模块)

内置模块

time模块

time模块是与时间相关的模块
time.sleep() -->延迟执行时间

import time
print('1111')
time.sleep(2.0) # 延迟执行指定的秒数。论点可能是用于次秒级精度的浮点数。
print('2222')
  • time.time() -->秒时间戳
print(time.time())  # 从纪元开始,以秒为单位返回当前时间。如果系统时钟提供,则可能出现秒的几分之一.
# 1970至今的秒时间戳
  • time.localtime() -->本地时间
print(time.localtime())  # seconds -->()
# 将纪元以来的秒转换为表示本地时间的时间元组。如果没有传入“seconds”,则转换当前时间。
  • time.strftime() -->接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。
'''
根据格式规范将时间元组转换为字符串。
2020-02-21 :
'''
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))

datetime也是与时间相关的模块

  • datetime.datetime.now() -->输出当前时间
import datetime
print(datetime.datetime.now())

在这里插入图片描述

random

random模块是随机模块

  • random.random() -->随机生成[0,1)的数
import random
print(random.random())  # [0,1)
  • random.randint() -->随机生成整数[]
print(random.randint(1,10))  # [a,b]
  • random.choice() -->随机在序列取元素
print(random.choice('12345'))  # 1/2/3/4/5
  • random.shuffle() -->打乱传入的容器内部顺序并返回
li = [1,2,3,4]
print(random.shuffle(li))  # 对列表x进行适当的洗牌,然后返回None。
print(li)
  • random.sample() -->随机取样
li = [1,2,3,4]
print(random.sample(li,2))  # 随机从li取两个
  • random.randrange() -->随机取整数[)
# 1,3,5,7,9-->range(1,10,2)
print(random.randrange(1,10,2))

random练习

实现6位数的随机验证码

'''
1.实现6位数的随机验证码
1-1.使用随机数字实现
'''
def v_code():
    code = ''
    for i in range(6):
        num = random.randint(0,9)
        # print(num)
        code += str(num)
    print(code)
    
    
v_code()
def v_code():
#     code = ''
#     for i in range(3):
#         li_range = [0,'1','2','A','B','C']
#         add_num = random.sample(li_range,2)  # []
#         add_num = map(str,add_num)
#         code += ''.join(add_num)
#     print(code)
# v_code()


'''
A-->ascii
ord() --> 字符 --> ascii
chr() --> ascii --> 字符
'''
# print(ord('A'))  # 65
# print(chr(65))
# print(ord('Z'))
def v_code_():
    code = ''
    for i in range(6):
        num = random.randrange(10)  # 从[0-9]
        chara = chr(random.randrange(65,91))  # [A-Z]
        add_num = random.choice([num,chara])
        code += str(add_num)
    print(code)
v_code_()

json模块

JSON是一种使用广泛的轻量数据格式。Python标准库中的json模块提供了JSON数据的处理功能。

由于JSON与python中的字典格式非常像。所以python中的json模块也相当于是用来使json与字典做转换。但是要注意的是,json中的数据必须使用双引号包裹。

  • json.loads() -->json转为字典(适用于语句)
import json
j_data = '{"name": "amy"}'  # json --> dict
# jsondecodeerror:期望属性名用双引号括起来
res = json.loads(j_data)
print(res['name'])
print(type(res))
  • json.dumps() -->字典转为json(适用于语句)
import json
data = {"name":"amy"}  # dict -->str
print(type(data))
# s = str(data)
# print(type(s))
res = json.dumps(data)  # dict --> json
print(res)              # '{"name": "amy"}'
print(type(res))
  • json.load() -->json转为字典(适用于文件)
  • json.dump() -->字典转为json(适用于文件)

新建包名为bao,继续在bao的包分别新建demo1.py、demo2.py
demo1.py

def test1():
    print("demo1_test1")


def test2():
    print("demo1_test2")

demo2.py

def test1():
    print("demo2_test1")


def test2():
    print("demo2_test2")

新建一个bao_test.py

# from bao import demo1,demo2
#
# demo1.test1()
# demo1.test2()
# demo2.test1()
# demo2.test2()

# from bao import *
#
# demo1.test1()
# demo1.test2()
# demo2.test1()
# demo2.test2()

import bao
bao.demo1.test1()

上面有些是完不成的,需要在bao的包自带的__init__.py

__all__ = ['demo1','demo2']  # 提供哪些是公开接口的约定

from . import demo1,demo2
发布了30 篇原创文章 · 获赞 0 · 访问量 686

猜你喜欢

转载自blog.csdn.net/luobofengl/article/details/104307177
今日推荐