190403内置模块

一、time&datetime模块

import time

print(time.time())  #1554370227.6189237 秒, 时间戳,从1970年1月1日 00:00开始到现在,1970年是Unix元年

t = time.localtime()  #结构化时间,本地时间
print(t)  #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=17, tm_min=39, tm_sec=40, tm_wday=3, tm_yday=94, tm_isdst=0)
print(t.tm_year)  #2019 ,取年份
print(time.localtime(1554371025.1826413))  #将当前时间转换为结构化时间

t2 = time.gmtime()  #结构化时间,UTC时间
print(t2)  #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=9, tm_min=39, tm_sec=40, tm_wday=3, tm_yday=94, tm_isdst=0)

print(time.mktime(time.localtime()))  #将结构化时间转换为时间戳

print(time.strftime("%Y-%m-%d %X", time.localtime()))  #2019-04-04 18:16:04  将结构化时间转换为字符串时间

print(time.strptime("2019-04-04 18:16:04", "%Y-%m-%d %H:%M:%S"))  #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=18, tm_min=16, tm_sec=4, tm_wday=3, tm_yday=94, tm_isdst=-1)  将字符串时间转换为结构化时间

print(time.asctime())  #将结构化时间转换为固定的格式, Mon Apr  8 15:47:59 2019

print(time.ctime())  #将时间戳时间转换为固定的格式, Mon Apr  8 15:48:48 2019

import datetime

print(datetime.datetime.now())  #2019-04-08 15:54:06.410871

二、random模块

import random

print(random.random())  #0 ~ 1之间的浮点数
print(random.randint(1,10))  #1 ~ 10之间的整数
print(random.randrange(1,10))  #1 ~ 10之间的整数,不包括10
print(random.choice([11,22,33]))  #在列表中随机取1个元素
print(random.sample([11,22,33],2))  #在列表中随机取2个元素
print(random.uniform(1,5))  #1 ~ 5之间的浮点数
l = [1,2,3,4,5]
random.shuffle(l)  #洗牌
print(l)  #[4, 1, 3, 5, 2]
  • 随机验证码代码示例
import random

def v_code():
    ret = ""
    for i in range(5):
        num = random.randint(0,9)
        alf = chr(random.randint(65,90))
        s = str(random.choice([num,alf]))
        ret += s
    return ret

print(v_code())

三、BASE_DIR

import sys,os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)  #D:\VirtualBox VMs\data\pyobj\1904
sys.path.append(BASE_DIR)
print(sys.path)  #['D:\\VirtualBox VMs\\data\\pyobj\\1904\\0404', 'D:\\VirtualBox VMs\\data\\pyobj\\1904', 'C:\\Python\\Python35\\python35.zip', 'C:\\Python\\Python35\\DLLs', 'C:\\Python\\Python35\\lib', 'C:\\Python\\Python35', 'C:\\Python\\Python35\\lib\\site-packages', 'D:\\VirtualBox VMs\\data\\pyobj\\1904']

四、os模块

import os

print(os.getcwd())  #获取当前工作目录,D:\VirtualBox VMs\data\pyobj\1904\0404
os.chdir("D:\\VirtualBox VMs\\data\\pyobj\\1904\\")  #cd命令
print(os.getcwd())  #D:\VirtualBox VMs\data\pyobj\1904

# 目录操作
os.makedirs("dir1/dir2/dir3")  #mkdir -p dir1/dir2/dir3
os.removedirs("dir1/dir2/dir3")  #rmdir -p dir1/dir2/dir3
os.mkdir("dir1")  #mkdir dir1
os.rmdir("dir1")  #rmdir dir1
print(os.listdir("0403"))  #ls -a

# 文件操作
os.remove("test.txt")  #删除一个文件
os.rename("test.txt", "new_test.txt")
print(os.stat("new_test.txt"))  #文件的元数据,os.stat_result(st_mode=33206, st_ino=9570149208162616, st_dev=2457279955, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1554785068, st_mtime=1554785068, st_ctime=1554785068)
print(os.path.getatime("new_test.txt"))
print(os.path.getmtime("new_test.txt"))
print(os.path.getctime("new_test.txt"))

# 其他
print(os.sep)  #路径分隔符
print(os.linesep)  #换行符
print(os.pathsep)  #路径分隔符,比如环境变量的配置
print(os.name)  #操作系统的名字
print(os.system("ipconfig"))  #执行操作系统命令

# 路径
print(os.path.split(os.getcwd()))  #('D:\\VirtualBox VMs\\data\\pyobj\\1904', '0404')
print(os.path.dirname(os.getcwd()))  #D:\VirtualBox VMs\data\pyobj\1904
print(os.path.basename(os.getcwd()))  #0404
print(os.path.exists("D:\\dir1\\test.txt"))  #判断路径是否存在
print(os.path.isabs("D:\\dir1\\test.txt"))  #是否是绝对路径
print(os.path.isfile("D:\\dir1\\test.txt"))  #是否是一个文件
a = "D:\\dir1"
b = "test.txt"
print(os.path.join(a,b))  #路径拼接,D:\dir1\test.txt

五、sys模块

import sys

print(sys.argv)  #命令行参数列表,第一个元素是程序本身路径,['D:/VirtualBox VMs/data/pyobj/1904/0404/模块.py']

print(sys.version)  #python版本,3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]

sys.stdout.write('#')  #打印到屏幕,#

sys.exit(1)  #退出程序

print(sys.path)  #打印环境变量

print(sys.platform)  #打印操作系统平台名称
  • 进度条
import time,sys
for i in range(30):
    sys.stdout.write('#')
    time.sleep(0.1)
    sys.stdout.flush()

六、json、pickle和shelve模块

  • json的应用(dumps和loads)
import json

dic = {"name":"dongfei", "age":18, "gender":"M"}
data = json.dumps(dic)
print(data, type(data))  #{"name": "dongfei", "age": 18, "gender": "M"} <class 'str'>

# with open("test.json", "w", encoding="utf-8") as f:
#     f.write(data)

with open("test.json", "r", encoding="utf-8") as f:
    new_data = json.loads(f.read())
    print(new_data,type(new_data))  #{'age': 18, 'name': 'dongfei', 'gender': 'M'} <class 'dict'>
  • json的应用(dump和load)
import json

dic = {"name":"dongfei", "age":18, "gender":"M"}

with open("test2.json", "w", encoding="utf-8") as f:
    json.dump(dic,f)

with open("test2.json", "r", encoding="utf-8") as f:
    new_data = json.load(f)
    print(new_data,type(new_data))  #{'gender': 'M', 'name': 'dongfei', 'age': 18} <class 'dict'>
  • pickle用法和json完全一样,但是pickle支持序列化函数和类,但是只能在python程序间使用,pickle文本不可读

  • shelve使用方法(pickle的高级封装)

import shelve

f = shelve.open(r"test4.txt")
f["stu1_info"] = {"name":"dongfei", "age":18, "gender":"M"}
print(f.get("stu1_info")["age"])  #18

七、xml模块

  • test5.xml
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
  • xml的操作
import xml.etree.ElementTree as ET

tree = ET.parse("test5.xml")
root = tree.getroot()  #获取根节点
print(root.tag)  #根标签名字,data

# 遍历xml文档
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag, i.text)  #text取内容

# 遍历year节点
for node in root.iter("year"):
    print(node.tag, node.text)

# 修改
for node in root.iter("year"):
    new_year = int(node.text) + 1
    node.text = str(new_year)  #修改内容
    node.set("update", "yes")  #修改属性值

tree.write("test6.xml")

# 删除
for country in root.findall("country"):  #findall查找文档中所有
    rank = int(country.find("rank").text)
    if rank > 50:
        root.remove(country)

tree.write("test7.xml")
  • 创建xml数据
import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")  #namelist  是根节点名

name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked":"no"})
sex = ET.SubElement(name, "sex")
sex.text = 'M'

name2 = ET.SubElement(new_xml, "name", attrib={"enrolled":"no"})
age = ET.SubElement(name2, "age")
age.text = "18"

et = ET.ElementTree(new_xml)  #生成文档对象
et.write("test8.xml", encoding="utf-8", xml_declaration=True)
ET.dump(new_xml)  #打印生成的文档对象
  • test8.xml
<?xml version='1.0' encoding='utf-8'?>
<namelist>
    <name enrolled="yes">
        <age checked="no" />
        <sex>M</sex>
    </name>
    <name enrolled="no">
        <age>18</age>
    </name>
</namelist>

八、re模块

1、正则表达式元字符

  1. . 任意一个字符
  2. ^,$ 行首/尾锚定
  3. * 0或无穷次
  4. + 1或无穷次
  5. ? 0或1次
  6. {3,5} 3或4或5次
  7. [] 字符集
[a-z]  表示所有小写字母
[^a-z]  表示所有小写字符外的字符
re.findall("\([^()]*\)","12+(23*5+2-5*(4-2))")  #['(4-2)']  匹配最里边的括号和内容
  1. \ 转义符
\d  匹配任何十进制数;它相当于类 [0-9]
\D  匹配任何非数字字符;它相当于类 [^0-9]
\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]
\S  匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]
\w  匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]
\W  匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\b  匹配一个特殊字符边界,比如空格 ,&,#等,单词锚定
>>> re.findall("I\\b","hello I am list")
['I']
>>> re.findall(r"I\b","hello I am list")  #r 的意思是原生字符串,里边的内容不需要让python解释器翻译
['I']
  1. | 或
  2. () 分组,将括号内的变成一个整体
>>> re.search(r"(?P<str_name>[a-z]+)\d+","hello33 I am list").group("str_name")  #后向引用
'hello'

2、re模块使用

import re

print(re.findall("\d+", "asdf123alsdkjflwk332alskjdfkl55kk"))  #查询字符串中所有匹配到的元素,并返回一个列表

print(re.search(r"(?P<str_name>[a-z]+)\d+","hello33 I am list").group("str_name"))  #匹配到第一个匹配到的元素,返回一个对象

print(re.match("abc","abc123abc"))  #相当于在search基础上加了^行首锚定

print(re.split("[,|]","abc,sdf,dfs|jkljks|sd"))  #使用特定的分隔符讲字符串分割后返回列表

print(re.sub("\d","B","abc123sdf33ds"))  #将匹配到的做替换操作,abcBBBsdfBBds

print(re.subn("\d","B","abc123sdf33ds"))  #('abcBBBsdfBBds', 5)

rule = re.compile("\d+")  #编译
print(rule.findall("asdf123alsdkjflwk332alskjdfkl55kk"))  #['123', '332', '55']

iter1 = re.finditer("\d+","asdf123alsdkjflwk332alskjdfkl55kk")  #返回一个迭代器对象
print(next(iter1).group())  #123

print(re.findall("www\.(baidu|qq|aliyun)\.com","www.baidu.com"))  #['baidu']
print(re.findall("www\.(?:baidu|qq|aliyun)\.com","www.baidu.com"))  #?:  去优先级,['www.baidu.com']

九、logging模块

  • basicConfig
import logging

logging.basicConfig(
    level=logging.INFO,  #定义日志记录级别
    filename="dongfei.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")
  • format参数中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s用户输出的消息
  • logger对象
import logging

def logger(logfile):
    logger = logging.getLogger("mylogger")
    logger.setLevel("INFO")  #设置日志级别

    fh = logging.FileHandler(logfile)  #向文件输出
    ch = logging.StreamHandler()  #向屏幕输出

    fm = logging.Formatter("%(asctime)s %(message)s")  #自定义格式

    fh.setFormatter(fm)
    ch.setFormatter(fm)

    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger

logger = logger("dongfei2.log")  #实例化

logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
logger.critical("critical message")

十、configparser模块

  • 应用
import configparser

config = configparser.ConfigParser()  #实例化

config["DEFAULT"] = {
    "ServerAliveInterval": 45,
    "Compression": "yes",
    "CompressionLevel": 9
}

with open("dongfei2.conf", "w") as f:
    config.write(f)
  • confile.conf
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no
import configparser

config = configparser.ConfigParser()

config.read("confile.conf")

# 查
print(config.sections())  #查询除default外的所有键['bitbucket.org', 'topsecret.server.com']

print(config["bitbucket.org"]["User"])  #查 ,hg

print(config["DEFAULT"]["CompressionLevel"])  #9

for key in config["bitbucket.org"]:  #无论遍历哪个键都会显示default段
    print(key)

print(config.options("bitbucket.org"))  #['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

print(config.items("bitbucket.org"))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

print(config.get("bitbucket.org","CompressionLevel"))  #9
  • 增删改
import configparser

config = configparser.ConfigParser()

config.read("confile.conf")

config.add_section("open.org")  #增键
config.set("open.org","k1","v1")  #增值

config.remove_section("topsecret.server.com")  #删键
config.remove_option("DEFAULT","compression")  #删值

config.set("DEFAULT","serveraliveinterval","56")  #改

config.write(open("confile2.conf","w"))

十一、hashlib模块

import hashlib

obj = hashlib.md5("with salt".encode("utf-8"))

obj.update("dongfei123".encode("utf-8"))

print(obj.hexdigest())  #ace17212e78240faf0773e40d8d4f268

十二、paramiko模块

  • 基于密码ssh连接
# Author:Li Dongfei
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.56.8', port=22, username='root', password='dongfei')
stdin, stdout, stderr = ssh.exec_command('df')
result = stdout.read()
ssh.close()
  • 基于密钥ssh连接
# Author:Li Dongfei
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
ssh = paramiko.SSHClient()
ssh.connect(hostname='192.168.56.8', port=22, username='root', pkey=private_key)
stdin, stdout, stderr = ssh.exec_command('df')
result = stdout.read()
print(result.decode())
ssh.close()
  • 基于密码sftp传输文件
# Author:Li Dongfei
import paramiko
transport = paramiko.Transport(('192.168.56.8', 22))
transport.connect(username='root', password='dongfei')
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get('/root/back.sql','back.sql')
transport.close()
posted @ 2019-04-18 21:25  生生不息.连绵不绝  阅读(202)  评论(0编辑  收藏  举报