动态导入模块

动态导入模块
导入一个库名为字符串的
module_t = __import__('m1.t')
print (module_t) #m1 import importlib
m=importlib.import_module('m1.t')
print (m) #m1.t

import

import 模块
1.执行对应文件
2.引入变量名 cal.py
print('ok')
def add(a,b):
return a+b
print('ok2') test.py
from cal import add #会先打印print('ok') print('ok2') 不支持 from web.web1 import web2
print (web2.cal.add(2,6)) if __name__ = "__main__":
用法1: 用于被调用文件的测试
用法2: 不想这个文件成为被调用文件

注意点

时间模块 time & datetime

import time

    掌握:
1.时间戳:
time.time() #从1970 1 1 到现在经过的秒数 2.当地时间
time.localtime() #time.struct_time(tm_year=2018, tm_mon=5, tm_mday=30, tm_hour=20, tm_min=11, tm_sec=38, tm_wday=2, tm_yday=150, tm_isdst=0)
t = time.localtime()
print(t.tm_wday) #周几 3.结构化时间
time.gmtime() 4.将结构化时间转化为时间戳
time.mktime(time.localtime()) #1527683203.0 5.将结构化时间转成字符串时间
time.strftime("%Y-%m-%d %X",time.localtime()) #2018-05-30 20:22:30 6.将字符串时间转成结构化时间
time.strptime("2018-05-30 20:22:30","%Y-%m-%d %X") #time.struct_time(tm_year=2018, tm_mon=5, tm_mday=30, tm_hour=20, tm_min=22, tm_sec=30, tm_wday=2, tm_yday=150, tm_isdst=-1) 7.直接看一个时间
time.asctime() #Wed May 30 20:27:36 2018 结构化转成字符串
time.ctime() #Wed May 30 20:27:36 2018 时间戳转成字符串 import datetime
datetime.datetime.now() #2018-05-30 20:34:39.589147

随机数模块

import random
random.random() #[0,1)
.......randint(1,3) #[1,3]
.......randrange(1,3) #[1,3)
.......choice([11,22,33]) #取一个
.......sample([11,22,33],2) #取二个 [22, 44]
.......unifom(1,3) #取一个 打乱顺序
ret = [11,22,33,44,55]
random.shuffle(ret) #[44, 11, 33, 22, 55] 一个应用 生成五位数随机数
def code():
ret = ""
for i in range(5):
num = random.randint(0,9)
s = random.randint(65, 90)
t = random.randint(97, 122)
alf = chr(random.choice([s,t])) # ASCII码转换
s = str(random.choice([num,alf]))
ret += s
return ret
print(code())

random

系统模块

import sys
sys.path.append() #临时修改环境变量
sys.platform() #返回操作系统平台名称
sys.path()
sys.exit(0)
sys.argv() #获取命令行参数 sys.stdout.write('#') #一次打出来 time.sleep()都没有用
sys.stdout.flush() #刷 有多少显示多少 进度条 应用1: 进度条案例
import sys
import time
for i in range(100):
sys.stdout.write('#')
time.sleep(0.1)
sys.stdout.flush() import os
os.path.dirname() os.cwd() #当前目录
os.chdir("test1") #转到子目录下的test1下 os.system('dir') #终端执行此命令
os.path.split() #分割成目录 和 文件名 os.path.join() #路径拼接 os.stat('test.py') #获取文件信息 os.path.getmtime() #最后一次修改时间

os sys

re模块

import re
re.search(r'\d','str46') #返回是一个对象
re.search(r'\d','str46').group() #取出值
#分组
re.search(r'(?P<name>[a-z]+)(?P<age>\d+)','tang36er34xiaoyang33').group('name','age') #('alex', '36') re.match('\d+','').group() #从开始处匹配 re.split("[ |]","hello abc|def") #空格 管道符匹配 re.split("[ab]","asdabcd") #
['', 'sd', '', 'cd'] re.sub('\d+','A',"jaskd4235ashdjf5423") #把匹配项改为A jaskdAashdjfA com = re.compile("\d") #写好规则
com.findall("str") re.finditer() re.findall("www\.(?:baidu|163)\.com","www.baidu.com") #不加?:只会显示 baidu 加了全部显示(取消括号优先级)

re

日志模块 logging

import logging

    logging.basicConfig(
level=logging.DEBUG,#修改级别为DEBUG
filename="logger.log", #存到文件中 默认是 追加模式
filemode="w", #修改为 写模式
format="%(asctime)s %(filename)s [%(lineno)d]) %(message)s" ) logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message') --------logger----
#子会打印父的内容
logger = logging.getLogger('mylogger') #无参默认是root用户 fh = logging.FileHandler("test_log")
ch = logging.StreamHandler() fm = logging.Formatter("%(asctime)s %(message)s")
fh.setFormatter(fm)
ch.setFormatter(fm) logger.addHandler(fh)
logger.addHandler(ch) #设置级别
logger.setLevel("DEBUG") #函数的话:
return logger
logger.debug("hello")
logger.info("hello")

logging

加密模块 hashlib

import hashlib  #位数是固定的
#obj = hashlib.md5("sb".encode('utf8')) #私钥
obj = hashlib.md5() #sha256一样
obj.update('hello'.encode('utf-8'))
print(obj.hexdigest()) #5d41402abc4b2a76b9719d911017c592

hashlib

Python开发【内置模块篇】的更多相关文章

  1. 《python开发技术详解》|百度网盘免费下载|Python开发入门篇

    <python开发技术详解>|百度网盘免费下载|Python开发入门篇 提取码:2sby  内容简介 Python是目前最流行的动态脚本语言之一.本书共27章,由浅入深.全面系统地介绍了利 ...

  2. python开发第一篇:初识python

    一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  3. Python开发第一篇

    Python 是什么? 首先他可能是比较好的一个编程开发语言!

  4. Python开发 第一篇 python的前世今生

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  5. python开发[第二篇]------str的7个必须掌握的方法以及五个常用方法

    在Python中 基本数据类型有 str int boolean list dict tuple等 其中str的相关方法有30多个 但是常用的就以下7个 join  # split # find # ...

  6. python开发第二篇 :python基础

    python基础a.Python基础      -基础1. 第一句python       -python后缀名可以任意?     -导入模块时如果不是.py文件,以后的文件后缀名是.py.2.两种 ...

  7. Python开发 基础篇

    2019-02-01 产生验证码: 用户输入的值和显示的值相同时显示Correct,否则继续生成随机验证码等待用户输入 def check_code(): import random checkcod ...

  8. Python开发第二篇

    运算符 1.算术运算符 % 取余运算符,返回余数 ** 幂运算符 //返回商的整数部分 2.逻辑运算符 and  与运算符 a and b 如果a为False是,表达式为False,如果a为True返 ...

  9. Python开发【第六篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  10. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

随机推荐

  1. redis 系列5 数据结构之字典(上)

    一. 概述 字典又称符号表(symbol table),关联数组(associative array), 映射(map),是一种用于保存键值对(key-value pair)的抽象数据结构.在字典中, ...

  2. 华为oj之字符串反转

    题目: 字符串反转 热度指数:4940 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串.例如: 输入描述: 输入N ...

  3. MongoDB Export & Import

    在使用MongoDB数据库的过程中,避免不了需要将数据进行导入和导出的工作,下面为具体的用法.注意 不同的数据库版本可能存在略微的差异,所以在使用时,先查看 --help 来进行确认.下面的为3.6版 ...

  4. leetcode — minimum-depth-of-binary-tree

    /** * Source : https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ * * * Given a binary t ...

  5. ES6躬行记(13)——类型化数组

    类型化数组(Typed Array)是一种处理二进制数据的特殊数组,它可像C语言那样直接操纵字节,不过得先用ArrayBuffer对象创建数组缓冲区(Array Buffer),再映射到指定格式的视图 ...

  6. python常用脚本以及问题跟踪

    1.时间操作//获取当前时间 格式是%Y-%m-%d %H:%M:%ScurrTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time. ...

  7. Spring Boot 2.x(十三):你不知道的PageHelper

    PageHelper 说起PageHelper,使用过Mybatis的朋友可能不是很陌生,作为一款国人开发的分页插件,它基本上满足了我们的日常需求.但是,我想去官方文档看看这个东西配合Spring B ...

  8. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU特性介绍(1)- 概览

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RT系列MCU的基本特性. ARM Cortex-M微控制器芯片厂商向来竞争激烈,具体可参看我的另一篇文章<第一 ...

  9. webpack4.0各个击破(5)—— Module篇

    webpack4.0各个击破(5)-- Module篇 webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决 ...

  10. 第8章 概述 - Identity Server 4 中文文档(v1.0.0)

    快速入门提供了各种常见IdentityServer方案的分步说明.他们从基础到复杂 - 建议您按顺序完成它们. 将IdentityServer添加到ASP.NET Core应用程序 配置Identit ...