我的python之路【第五章】内置函数

内置模块:

  1.time模块和datetime模块

  2.os模块

  3.sys模块

  4.random模块(取随机数)

  5.pickle模块(序列化)

  6.shelve模块

  7.json模块

  8.configparser模块

  9.hashlib模块 

  10.subprocess模块

 

 

内置函数:

#将不可变的字符串,转换为可以通过ascii码修改的
bytearray()
#是否可以调用
callable('a')
#转换为asccii
chr(15)
#16进制
hex(10)
oct(1)
ord('o')
#浮点
float()
#exec将字符串形式的代码解析并执行
a="print('123')"
exec(a)
#eval将表达式的字符串形式的表达式进行运算
b='1+2-3*9'
eval(b)
#解释器使用,import实现底层
#compile()
#compile    把一个代码文件加载进来,按exec,or,eval的方式解析并执行。
f = open("函数递归.py")
data =compile(f.read(),'','exec')
exec(data)
dict()#字典
#查看对象的方法
dir()
#返回余数和商
divmod()
#filter 过滤函数返回值  打印大于5的值
a=range(10)
b=filter(lambda  x:x>5,a)
for i in b :print(i)
#map 对返回值进行操作
a=map(lambda x:x*x,range(10))
for i in a :print(i)
#reduce
from functools import reduce
a=reduce(lambda x,y:x+y,range(10))
print(a)
print(list(range(10)))
#字符串格式化
format()
#将数据变成只读。
a={1,2,4,34,3,3}
frozenset(a)
#globals() #将当前程序在内存中所有全局变量都以字典的形式打印出来。
globals()
#locals() 将当前程序在内存中所有局部变量以字典的形式大于出来
locals()
#hash() #保证字符在当前程序下是唯一的。
hash('alex')
#iter()

#memoryview()
#在进行切片并赋值数据时,不需要重新copy原列表数据,可以直接映射原数据内存。
#对比,直接修改内存地址,修改变量和赋值修改变量。
import time
for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = data
    while b:
        b = b[1:]
    print('bytes', n, time.time()-start)

for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = memoryview(data)
    while b:
        b = b[1:]
    print('memoryview', n, time.time()-start)

#print()进度条
import time
for i in range(10):
    print('#',end='',flush=True)
    time.sleep(0.5)
#print 写入文件
f=open("file.txt",'w',encoding='utf-8')
print("hey",file=f)
f.close()
#repr,返回字符串。转换为字符串形式
def hey():pass
a=repr(hey)
#反转reversed
a=[1,2,3,4]
print(reversed(a))
#round 保留几位小数
a=3.12384945
round(a,2)
#sice,切片.规定一个切片格式。
a=range(20)
pattern=slice(3,8,2)
for i in a[pattern]:
    print(i)
#sorted

#vars 将所有的变量打印出来
#zip  #拉链
a=list(range(10))
b=list(range(10,16))
print(zip(a,b))

#__import__()
#导入用户输入的模块,用户输入是字符形式
__import__("123")
View Code

time模块和datetime模块

import time
print(time.time())  #时间戳,从计算机开始的秒数。1970-1-1 开始
print(time.altzone/60/60,)   #返回时间与utc时间的时间差,以秒计算
print(time.asctime())
t=time.localtime()  #返回本地时间:struct time对象格式
print(t.tm_year,t.tm_mon)
print(time.gmtime(time.time() - 800000))  # 返回utc时间的struc时间对象格式

#自定义时间展示格式:当前时间
print(time.strftime("%Y-%m-%d %H:%M:%S"))
#前一天的时间展示
struct_time=time.localtime(time.time()-86400)
print(time.strftime("%Y-%m-%d %H:%M:%S",struct_time))

#将字符串转成时间;
time_str='2017-02-18 15:27:23'
#1、转换成时间对象
struct_time=time.strptime(time_str,"%Y-%m-%d %H:%M:%S")
print(struct_time)
#2、把一个时间对象转换成时间戳
print(time.mktime(struct_time))
#有一个图

import datetime
#打印当前时间
print(datetime.datetime.now())
#将时间戳直接转换成字符串
print(datetime.datetime.fromtimestamp(time.time()))
#直接加减天数,默认是days. 参数:hours minutes
print(datetime.datetime.now()-datetime.timedelta(3))
print(datetime.datetime.now()-datetime.timedelta(hours=2,minutes=30))
#时间的替换
print(datetime.datetime.now().replace(year=2016,month=3))

 os模块

import os
os.getcwd() #获取当前目录
os.chdir()  #切换目录
os.curdir   #返回当前目录
os.pardir   #获取当前目录的父目录字符串名
os.makedirs("d1/d2/d3") #创建多级目录
#如果文件存在,会报错。加入下面参数就不报错了
os.makedirs("d1/d2/d3",exist_ok=True)
os.listdir('.') #列出指定目录的文件
os.stat('') #查看文件详细属性
os.sep  #输出操作系统特定的路径分割符
os.linesep  #输出当前平台使用的行终止符。
os.name     #输出字符串指示当前系统
import platform
print(platform.system())    #查看操作系统简写
#system 每次调用会打开一个终端去执行
os.system("df -h")  #输出,但不会保存。只会返回命令执行结果的状态
os.popen("df -h").read()    #可以拿到命令的执行结果。
os.environ  #获取系统环境变量
os.path.abspath(__file__)   #通过相对路径返回程序的绝对路径 __file__ 是文件的相对路径
os.path.dirname()   #返回上一级目录
os.path.exists("path")#如果path存在返回True,不存在返回False

sys模块

import sys
#返回python的搜索路径
print(sys.path)
#python支持的最大int值
print(sys.maxsize)
#打印系统
print(sys.platform)
#标准输出,打印到屏幕
print(sys.stdout.write("sjka"))
val=sys.stdin.readline()
print(val)

 

random随机数

import random
#在1-10中取随机数
print(random.randint(1,10))
#在1-20 步长为2中去随机数。奇数
print(random.randrange(1,20,2))
#取5个随机数
print(random.sample(range(100),5))

low版本:

'''生成随机数进行验证'''
import random
checkcode = ''
for i in range(4):  #循环4次,生成4个随机数
    current = random.randrange(0,4) #从0-3中选取一个随机数
    if current != i:                #如果随机数与正在循环的次数不相等,随机一个字母
        temp = chr(random.randint(65,90))
    else:                           #相等,随机一个数字
        temp = random.randint(0,9)
    checkcode += str(temp)
print(checkcode)

高级精简版

'''随机验证高级精简版'''
import random,string
#string.digits 0-9的整数
# string.ascii_uppercase 大写的ascii码字符
source=string.digits+string.ascii_uppercase
#source : 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
print("".join(random.sample(source,6)))

pickle模块:序列化

#序列化: 内存---> 字符串
#反序列化:字符串--->内存
#pickle模块,只适用于python内部的序列化,其它开发语言不能识别
#pickle 序列化过程
import pickle
account={
    'id':575163273,
    "credit":15000,
    "balance":8000,
    "expire_date":"2020-5-20",
    "password":"123456"
}
f=open("account.db",'wb')
#f.write(pickle.dumps(account))
pickle.dump(account,f)
f.close()
'''pickle反序列化'''
import pickle
f=open("account.db",'rb')
#acount=pickle.loads(f.read())
account=pickle.load(f)
print(account)
f.close()

 

shelve模块
 多个数据进行序列化:

#只适合于python之间用,底层使用pickle实现
import shelve
d=shelve.open('shelve_test')
#生成序列化的文件,3个,不需要管理
info = {'age':22,'job':'it'}    #持久化字典
name = ['alex','oldboy','testsss'] #持久化列表
d['name']=name
d['info']=info
d.close()

这个模块会自动生成3个文件:

shelve_test.bak
shelve_test.dat
shelve_test.dir

#读取保存好的数据
d=shelve.open('shelve_test')
#输出序列化的文件
print(d.get('name'))
print(d.get('info'))

 

json模块

#json支持跨版本jave,c语言编程。-----支持简单的数据类型
#pickle只能在python本语言内进行。-------支持一切数据类型,包括函数
'''json序列化1,'''
import json
info = [1,2,3,4,5,6]
f=open('json.txt','w',encoding='utf-8')
f.write(json.dumps(info))
f.close()
'''json反序列化1,'''
import json
f=open('json.txt','r',encoding='utf-8')
d=json.loads(f.read())
print(d)
'''json序列化2'''
import pickle
info = {
    'name':'alex',
    'age':'22'
}
f=open('json.test','wb')
pickle.dump(info,f)     #等价于f.write( pickle.dumps(info))
f.close()
'''json反序列化2'''
import pickle
f=open('json.test','rb')
a=pickle.load(f)            #a=pickle.loads(f.read())
print(a['age'])

下面使用方法是错误示范-----注意;

'''
json过程中,只进行一次dump,只进行一次load----------错误示范向下
'''
import json
info = {
    'name':'alex',
    'age':'22'
}
f=open('json.test','w',encoding='utf-8')
f.write( json.dumps(info))
info['age'] ='88'
f.write( json.dumps(info))
f.close()
'''
json过程中,只进行一次dump,只进行一次load
'''
#########################################
#没有办法进行多次load
import json
f=open('json.test','r',encoding='utf-8')
a=json.loads(f.read())
b=json.loads(f.read())
f.close()
print(a['age'])
print(a['name'])

configparser模块

作用:生成配置文件

#注意.下面 遵循字典的格式
import configparser
#生成config对象
config = configparser.ConfigParser()
#写配置文件第一级['DEFAILT']
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}
#
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
#将配置写入文件中
with open('example.ini', 'w') as configfile:
    config.write(configfile)

生成后的配置文件:

[DEFAULT]
compressionlevel = 9
compression = yes
serveraliveinterval = 45
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

读取配置文件:

#读取配置文件
import configparser
config=configparser.ConfigParser()
#使用内置的方法去读取配置文件
config.read('example.ini')
value1=config.get('DEFAULT','compression')
value2=config.get('topsecret.server.com','host port')
print(value1)
print(value2)

修改配置文件:

#修改配置文件
import configparser
#生成一个config对象
config=configparser.ConfigParser()
#读取配置文件
config.read('example.ini')
#创建一个新的配置选项
# config.set("DEFAULT",'name','liuhao')  #两种设置的方式
config['DEFAULT']['name']='liuhao'
#将配置重新写入到配置文件
with open('example.ini', 'w') as configfile:
    config.write(configfile)

修改后的配置文件

[DEFAULT]
compressionlevel = 9
compression = yes
serveraliveinterval = 45
forwardx11 = yes
name = liuhao

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no
View Code

hashlib模块 

MD5加密

import hashlib
m = hashlib.md5()   #声明一个实例
m.update(b'hello')
print(m.hexdigest())    #输出一段md5值 16进制
m.update(b' my name is alex')   #相当于两段字符串拼接,进行做md5
print(m.hexdigest())
m2 =hashlib.md5()
m2.update(b'hello my name is alex') #验证拼接后的,进行md5验证
print(m2.hexdigest())   #此处会发现----两次校验后的值是一样的
print(m2.digest())#二进制的
#输出结果
5d41402abc4b2a76b9719d911017c592
fcb0ddbd764ea1759efc05954cba60b0
fcb0ddbd764ea1759efc05954cba60b0
b'\xfc\xb0\xdd\xbdvN\xa1u\x9e\xfc\x05\x95L\xba`\xb0'

SHA加密

#SHA加密
hash=hashlib.sha1() #谷歌破解了sha1 加密方式
hash.update(b'admin')
print(hash.hexdigest()) #16进制 加密值
hash=hashlib.sha256()   #sha256 加密方式
hash.update(b'admin')   
print(hash.hexdigest()) 
hash=hashlib.sha512()   #sha512 加密方式
hash.update(b'admin')
print(hash.hexdigest())
#输出结果
d033e22ae348aeb5660fc2140aec35850c4da997
8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec

hmac 网络消息验证

#hmac  碉堡了 ,网络消息验证
import hmac
#前面是key 后面是内容
h=hmac.new('天王盖地虎'.encode(),'宝塔镇河妖'.encode())
print(h.hexdigest())
#对方有key 。会收到内容和加密后的值。
#对方直接使用key+ 内容 进行加密,对应得数据,一致---数据未被篡改

 re模块

#liuhao
'''re正则匹配'''
'''
'.'     默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
'^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
'$'     匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
'*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
'+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
'?'     匹配前一个字符1次或0次
'{m}'   匹配前一个字符m次
'{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
'|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
'(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c


'\A'    只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
'\Z'    匹配字符结尾,同$
'\d'    匹配数字0-9
'\D'    匹配非数字
'\w'    匹配[A-Za-z0-9]
'\W'    匹配非[A-Za-z0-9]
's'     匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'

'(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}
'''
#最常用的匹配语法
'''
re.match 从头开始匹配         默认包含^                       查看re.match('^a','sjsa').group
re.search 匹配包含            查找到一个结果就会自动返回。
re.findall 把所有匹配到的字符放到以列表中的元素返回。            没有group方法
re.splitall 以匹配到的字符当做列表分隔符
re.sub      匹配字符并替换
'''
import  re
a=re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city")
#(>P<key>)固定用法,后面匹配到的值为value.得到一个字典
print(a)
#以数字为分割符,[0-9]+1个或多个数字
b=re.split('[0-9]+','asja121jskdf85jc92mf8d0s7f')
print(b)
#替换,将匹配到的内容替换为'|',
b=re.sub('[0-9]+','|','asja121jskdf85jc92mf8d0s7f')
print(b)
#只匹配两次
b=re.sub('[0-9]+','|','asja121jskdf85jc92mf8d0s7f',count=2)
print(b)

xml

#liuhao
import xml.etree.ElementTree as ET
#输出后需要自己修改文件格式。排版
new_xml = ET.Element("namelist")#根节点
personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "yes"})#子节点
name=ET.SubElement(personinfo,"name")
name.text = "Alex Li"
age = ET.SubElement(personinfo, "age", attrib={"checked": "no"})    #为age标签添加属性
sex = ET.SubElement(personinfo, "sex")
age.text = '33'     #设置文本信息
personinfo2 = ET.SubElement(new_xml, "personinfo2", attrib={"enrolled": "no"})
name=ET.SubElement(personinfo2,"name")
name.text = "Oldboy"
age = ET.SubElement(personinfo2, "age")
age.text = '19'

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式

生成的文件:

#创建后xml文件
<?xml version='1.0' encoding='utf-8'?>
<namelist>
    <personinfo enrolled="yes">
        <name>Alex Li</name>
        <age checked="no">33</age>
        <sex />
    </personinfo>
    <personinfo2 enrolled="no">
    <name>Oldboy</name>
    <age>19</age>
    </personinfo2>
</namelist>

处理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>
View Code
import xml.etree.ElementTree as ET

tree = ET.parse("xml_src")
root = tree.getroot()
print(root.tag)

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

# 只遍历year 节点
for node in root.iter('year'):
    print(node.tag, node.text)
import xml.etree.ElementTree as ET

tree = ET.parse("xml_src")
root = tree.getroot()
#
# # 修改
# for node in root.iter('year'):
#     new_year = int(node.text) + 1
#     node.text = str(new_year)
#     node.set("updated", "yes")
#
# tree.write("xml_src")

# 删除node
for country in root.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)
tree.write('xml_out.xml')

subprocess模块

用途:执行系统命令

import subprocess
# 执行简单命令
subprocess.run(['df','-h'])
# 执行复杂命令,指定不需要python解析。直接扔给shell 终端 执行
subprocess.run('df -h|grep /dev/sda1',shell=True)
# 获取执行状态,和返回结果
a=subprocess.getstatusoutput('pwd')
print(a)

输出a的值

#输出是一个元组格式---1 是命令执行的状态,2 命令返回的结果
(0, '/home/liuhao')
#输出一个执行的命令,直接交给终端执行,将标准输出和标准错误输出区分开
>>> res=subprocess.Popen("pwd",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#输入一个正确的命令,打印标准输出,注意需要根据系统编码格式,进行转码
>>> res.stdout.read().decode('utf-8')                                            '/home/liuhao\n'
#打印标准错处输出
>>> res.stderr.read().decode('utf-8')                                            ''
>>> res=subprocess.Popen("pwwwd",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> res.stderr.read().decode('utf-8')                                            '/bin/sh: 1: pwwwd: not found\n'
>>> res.stdout.read().decode('utf-8')                                            ''

 # 最近遇到一个问题,需要获取命令的执行状态返回值,找到了这样两个方法

1、subprocess.call
subprocess.call (*popenargs , **kwargs )
执行命令,并等待命令结束,再返回子进程的返回值。参数同Popen,查看/usr/lib/python2.7/subprocess.py 

res = subprocess.call('ping -c 3 -i 0.2 -W 1 192.168.10.1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#获取执行结果
In [21]: res.real
Out[21]: 1

2、subprocess.check_call
subprocess.check_call (*popenargs , **kwargs )
执行上面的call命令,并检查返回值,如果子进程返回非0,则会抛出CalledProcessError异常,这个异常会有个returncode 
属性,记录子进程的返回值。

# 执行错误的时候直接报错,需要捕获异常进行处理
In [27]: res = subprocess.check_call('ping -c 3 -i 0.2 -W 1 193.1.1.1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-27-7709f7d781b3> in <module>()
----> 1 res = subprocess.check_call('ping -c 3 -i 0.2 -W 1 193.1.1.1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

/usr/local/python34/lib/python3.4/subprocess.py in check_call(*popenargs, **kwargs)
    556         if cmd is None:
    557             cmd = popenargs[0]
--> 558         raise CalledProcessError(retcode, cmd)
    559     return 0
    560 

CalledProcessError: Command 'ping -c 3 -i 0.2 -W 1 193.1.1.1' returned non-zero exit status 1

# 执行正常的时候
In [28]: res = subprocess.check_call('ping -c 3 -i 0.2 -W 1 192.168.10.1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

 

posted @ 2017-02-20 20:59  saynobody  阅读(565)  评论(1编辑  收藏  举报