python常用模块与pip

  • 常用模块

    time模块

    函数名 函数说明
    time() 返回当前秒数
    localtime() 接收时间戳 返回当前时间的元组 0代表周一
    strftime("%Y-%m-%d %H:%M:%S") 接收时间元组 返回可读的字符串表示当地时间
    asctime() 返回格式化后的英文的时间
    mktime() 接收时间元组 返回时间戳
    sleep(seconds) 推迟线程调用的执行(进行阻塞)
    strptime("2021-8-25 11:15:00", '%Y-%m-%d %H:%M:%S') 将给定时间转换为时间元组
    符号 符号说明
    %Y 四位的年
    %y 两位的年
    %m 月份(01-12)
    %d 月份内的某一天(1-31)
    %H 24小时制的时间(0-23)
    %l 12小时制的小时数(01-12)
    %M 分钟数(0-59)
    %S 秒 (0-59)
    %a 本地简化星期的名称
    %j 年内的一天(011-366)
    %w 星期 (0-6)
    %x 本地相应的日期表示
    %X 本地相应的时间表示

    datetime模块

    函数名 说明
    datime.now() 获取当前时间
    datetime.datetime() 获取指定的日期时间 datetime.datetime(2021, 8, 25, 8, 8, 8)
    datetime.fromtimestamp(time.time()) 时间戳转换为时间
    datetime.datetime.now().strftime('%X') 将datetime对象转换为字符串

    日历

    import calendar

    函数 说明
    calendar.month() 返回指定年的月份
    calendar.calendar() 返回指定年的日历
    calendar.isleap() 判断是否为闰年

    collection模块

    from collections import nametuple

    • namedtuple 创建了一个新的数据类型

    Point = namedtuple('point', ['x', 'y'])    # 创建了point类
     # print(point)  
     new_p = Point(1, 2)  #  实例化
     # print(new_p)
     # 获取值
     print(new_p[0])
     print(new_p[1])
     print(new_p.x)
     print(new_p.y)
     ​
     ​
     '''
     <class '__main__.point'> 
     point(x=1, y=2)
     1
     2
     1
     2
     '''
    • deque 高效插入和删除元素的双向列表 适用于队列和栈

    from collections import deque
     q = deque([1, 2, 3])
     print(q)
     q.append(4)
     print(q.pop())
     print(q.popleft())
    • Counter 统计字符串中每个字符出现的次数

    from collections import Counter

    from collections import Counter
     String = 'lucky is a good man'
     c = Counter()
     # print(c)
     # print(type(c))
     for s in String:
         c[s] = c[s] + 1
     ​
     # print(c)
     for k in c:
         print(k, c[k])

    hashlib模块

    是最常见的加密算法 速度比较快 生产32位的十六进制的加密

    import hashlib
     s1 = b'lucky is a good man'
     m1 = hashlib.md5()
     # print(m1)
     m1.update(s1)
     print(m1.hexdigest())

    三方模块

    • pip install 模块名 == 版本 按照指定版本号安装第三方模块

    • pip install 模块名 -i 安装源 按照指定安装源安装第三方模块

    • pip uninstall 模块名 卸载模块

    • pip list 查看安装的模块

    • pip -V 查看pip版本

    • pip show 模块名 查看安装包存放位置

posted @ 2021-08-25 20:29  wq512  阅读(101)  评论(0编辑  收藏  举报