Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识

今日内容:

1.hash模块
2.xml模块
3.configparser模块
4.sheve 模块
5.shutil模块

 

知识点一:hash
什么是hash:
 hash是一种算法,该算法接受传入的的内容,经过运算得到一串hash如果把hash算法比喻一座工厂
 那传给hash算法的内容就是原材料,生产的hash值就是生产出的产品
 
为何用hash算法:
 hash值产品有三大特性:
 1.只要传入的内容一样,得到的hash值必然是一样的
 2.只要我们使用的hash算法固定,无论传入的内容有多大得到的hash值得长度是固定的
 3.不可以用hash值逆推原来的内容
 基于1和2可以在下载文件时做文件一致性校验
 基于1和3可以对密码进行加密
 
 
# 例如:
import hashlib
password=input('密码:')
m=hashlib.md5('天王盖地虎'.encode('utf-8'))  #可以多一层复杂性加密
m.update(password.encode('utf-8'))
print(m.hexdigest())
'''
结果:
密码:123
41046ee2686f6c698c859a13b47cdb1f
'''


import hashlib
# 用法1:
#1.造工厂
m=hashlib.md5()     #m=hashlib.sha256() 可以是其他加密个是
#2.运送材料
m.update('你好啊'.encode('utf-8'))
#3.产出hash值
print(m.hexdigest())  #124756ef340daf80196b4124686d651c

#用法2:
m=hashlib.md5('你'.encode('utf-8')) #可以在造工厂的时候就添加材料
m.update('好啊'.encode('utf-8')) 
print(m.hexdigest())  #124756ef340daf80196b4124686d651c   hash结果:和上面一样,因为传入的材料是一样的

 

 

知识点二:xml
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,
但json使用起来更简单
xml的格式如下,就是通过<>节点来区别数据结构的:

xml模块:
 1.标签名  root.tag
 2.便签属性 root.attrib
 3.标签文本 root.text

以xml.xml文件为例:
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

可以对xml文件进行一下操作:
import xml.etree.ElementTree as ET

tree=ET.parse('xml.xml')        #parse单词:从语法上分析 理解
root = tree.getroot()

#对任何标签都有三个特征:便签名、标签属性、标签的文本内容
print(root.tag)    #data
print(root.attrib)  #标签属性 {}
print(root.text)  #标签文本  空

print(list(root.iter('year')))
print(root.iter('year'))

for year in root.iter('year'):
    print(year.tag)
    print(year.attrib)
    print(year.text)
    print('========================')

# 在root的子节点找,只找一个
print(root.find('country').attrib)     #{'name': 'Liechtenstein'}
print(root.findall('country'))     #[<Element 'country' at 0x055F5CC0>,.. 列表格式
# 在root的子节点找,找所有
# 列表推导式,找出所有二级country本身的属性
print([country.attrib for country in root.findall('country')])
for country in root.findall('country'):
   print(country.attrib)
# 结果:
'''
{'name': 'Liechtenstein'}
{'name': 'Singapore'}
{'name': 'Panama'}
'''

# 1.查
# 遍历整个文档
for country in root:
    # print('========>国家%s'%country.attrib)
    for item in country:
        print(item.tag)                                             #year    #rank
        print(item.attrib)      #<year>2008</year>属性标签为空:{}  #{}      #{'updated': 'yes'}
        print(item.text)   #<year>2008</year>文本标签为2008         #2018    #2

# 2.改
for year in root.iter('year'):
    print(year.tag)  #year year year
    year.attrib={'update':'yes'}
    year.text=str(int(year.text)+1)

tree.write('xml.xml')

# 3.增加
for country in root:
    rank=country.find('rank')
    if int(rank.text)>50:
        tag=ET.Element('egon')        #element单词意思:元素   是否意思为增加一个名为egon的标签???
        tag.attrib={'update':'yes'}
        tag.text='NB'
        country.append(tag)

tree.write('xml.xml')

# 4.删除
for country in root:
    tag=country.find('egon')
    # print(tag)             #前两个country下面没有egon,所有没提示 None
    if tag is not None:
        print('====>')
        country.remove(tag)
tree.write('xml.xml')

 


知识点三:configparser模块(解析配置文件)
主要所有三项:
 1.config.sections  查看标题
 2.config.options   查看指定标题下面所有key=value的key值
 3.config.get       查看指定标题下面key=value的value值
 4.config.items     查看取所有key、value的值以(key,value)格式显示

以文件config.ini格式为例:
[egon]
sex='female'
age=20
salary=31
is_auth=True

[alex]
sex='male'
age=20
salary=1
is_auth=True

可以进行一下操作:
import configparser
config=configparser.ConfigParser()
config.read('config.ini')

取标题
print(config.sections())   # ["'egon'", "'alex'"]

取文件标题下面下所有key=value的key
print(config.options('egon'))  #['sex', 'age', 'salary', 'is_auth']

取文件标题下面指定的key=value的value
print(config.get('egon','age'))  #20

取所有key=value的(key,value)格式
print(config.items('egon'))
[('sex', "'female'"), ('age', '20'), ('salary', '31'), ('is_auth', 'True')]

 

 

 


知识点四:sheve 模块(序列化和反序列化)
shelve更简单,也支持所有的的数据类型,但只能在python里面用
import shelve

f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
f['stu2_info']={'name':'gangdan','age':53}
1.存文件
f=shelve.open(r'shelve.txt')

2.取文件
print(f['stu1_info']['hobby'])
print(f['stu2_info']['name'])

3.改文件内容
注意点:
f['stu1_info']['age']=44444  这样看是赋值改动,但是实际没有改,因为没有写入的文件
print(f['stu1_info'])
要想写入,需要添加,writeback=True 将修改的文件写回后台文件
f=shelve.open(r'shelve.txt',writeback=True)
f['stu1_info']['age']=44444
print(f['stu1_info'])
'''
输出结果为:
{'name': 'egon', 'age': 44444, 'hobby': ['piao', 'smoking', 'drinking']}

 

 

 


知识点五:shutill模块
高级的 文件、文件夹、压缩包 处理模块
import shutil

拷贝文件
方式一:
with open('config.ini','r')as read_f,open('new.xml','w') as write_f:
    shutil.copyfileobj(read_f,write_f)

方式二:shutil.copyfile(src, dst)
源文件事先定义好,目标文件无需存在,
shutil.copyfile('new.xml', r'E:\f2.log') #拷贝到指定文件
shutil.copyfile('new.xml', 'f2.log')  #拷贝到当前文件夹


仅拷贝文件权限,内容、组、用户均不变  shutil.copymode(src, dst)
目标文件均不变,只是文件权限变动
shutil.copymode('new.xml', 'f2.log')

仅拷贝状态信息,包括:mode bits, atime, mtime, flags
shutil.copystat('new.xml', r'E:\f2.log')

拷贝文件和权限
import shutil
shutil.copy('f1.log', 'f2.log')

递归的去拷贝文件夹
import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,
# 注意对folder2目录父级目录要有可写权限,ignore的意思是排除

递归的取删除文件
import shutil
shutil.rmtree('folder1')

#递归的去移动文件夹 shutil.move(src, dst)
import shutil
shutil.move('folder1', 'folder3')

创建压缩包并返回文件路径
import shutil
'''
1.base_bak: 压缩后文件的名字,压缩包的文件名(也可以指定压缩好保存的具体文件目录)
    如 data_bak=>保存至当前路径
    如:/tmp/data_bak =>保存至/tmp/
2.gztar: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
3.root_dir: 被压缩文件的路径(默认当前目录)
4.owner: 用户,默认当前用户
5.group: 组,默认当前组
6.logger: 用于记录日志,通常是logging.Logger对象
'''
#res=shutil.make_archive('data_bak','gztar',root_dir=r'E:\PycharmProjects\untitled\day17\包练习')

#解压文件(解压上面刚刚压缩的文件)
import tarfile
t=tarfile.open(r'E:\PycharmProjects\untitled\day20\data_bak.tar.gz','r') #源文件路径
t.extractall(r'E:\PycharmProjects\untitled\day20\dir_tarfile') #解压后文件存放路径
t.close()

 

知识点六:面向对象

面向对象编程:
对象:特征与技能的集合体,上帝的思维方式

优点:
 可扩展性强
缺点:
 编程的复杂程度要高于面向过程


类;类是一系列具有相同特征、技能对象的集合体
强调:站的角度不同,总结出来的来是截然不同的

现实世界中:先有对象,后才有类
在程序中:必须先定义类,后调用类来产生对象

《类里面尽量用驼峰体》


面向对象初始模板:
class OldboyStudent:   #类的名称OldboyStudent
    school='Oldboy'    #特征(变量表示)

    def learn(self):   #就是一个普通函数
        print('is learn skill')  #技能1(函数表示)


    def choice(self):
        print('choose course')   #技能2(函数表示)

print(OldboyStudent)  #<class '__main__.OldboyStudent'>
print(OldboyStudent.__dict__)  #输出结果:如下
{'__module__': '__main__', 'school': 'Oldboy', 'learn':
<function OldboyStudent.learn at 0x05A9D810>, 'choice': <function OldboyStudent.choice at 0x05A9D7C8>,
'__dict__': <attribute '__dict__' of 'OldboyStudent' objects>, '__weakref__': <attribute '__weakref__' of
 'OldboyStudent' objects>, '__doc__': None}
Oldboy
print(OldboyStudent.__dict__['school'])   #'school'是以字符串传值得
print(OldboyStudent.school)
OldboyStudent.learn(123)  #OldboyStudent.learn('aaa')

注意理解:
OldboyStudent.learn(123) .后面跟的是类里面的属性,可以是变量名school和函数名learn

类的代码会在类定义阶段就立即执行,会产生一个类的名称空间
类的本身就是一个容器/名称空间,是用来存放名字的,这是类的用途之一

转载于:https://www.cnblogs.com/yangzhizong/p/9225668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值