zoukankan      html  css  js  c++  java
  • python- python内置模块 面向对象

    1.configparser模块

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件

    复制代码
    # 注释1
    ;  注释2
     
    [section1] # 节点
    k1 = v1    # 值
    k2:v2       # 值
     
    [section2] # 节点
    k1 = v1    # 值
    
    指定格式
    复制代码

    1.2、获取所有节点

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.sections()
    print(ret)

    1.3、获取指定节点下所有的键值对

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.items('section1')
    print(ret)

    1.4、获取指定节点下所有的键

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.options('section1')
    print(ret)

    1.5、获取指定节点下指定key的值

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
     
    v = config.get('section1', 'k1')
    # v = config.getint('section1', 'k1')
    # v = config.getfloat('section1', 'k1')
    # v = config.getboolean('section1', 'k1')
    复制代码

    1.6、检查、删除、添加节点

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
    # 检查
    has_sec = config.has_section('section1')
    print(has_sec)
     
    # 添加节点
    config.add_section("SEC_1")
    config.write(open('xxxooo', 'w'))
     
    # 删除节点
    config.remove_section("SEC_1")
    config.write(open('xxxooo', 'w'))
    复制代码

    1.7检查、删除、设置指定组内的键值对

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
    # 检查
    has_opt = config.has_option('section1', 'k1')
    print(has_opt)
     
    # 删除
    config.remove_option('section1', 'k1')
    config.write(open('xxxooo', 'w'))
     
    # 设置
    config.set('section1', 'k10', "123")
    config.write(open('xxxooo', 'w'))
    复制代码

    2.XML模块

    XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下

    复制代码
    <data>
        <country name="Liechtenstein">
            <rank updated="yes">2</rank>
            <year>2023</year>
            <gdppc>141100</gdppc>
            <neighbor direction="E" name="Austria" />
            <neighbor direction="W" name="Switzerland" />
        </country>
        <country name="Singapore">
            <rank updated="yes">5</rank>
            <year>2026</year>
            <gdppc>59900</gdppc>
            <neighbor direction="N" name="Malaysia" />
        </country>
        <country name="Panama">
            <rank updated="yes">69</rank>
            <year>2026</year>
            <gdppc>13600</gdppc>
            <neighbor direction="W" name="Costa Rica" />
            <neighbor direction="E" name="Colombia" />
        </country>
    </data>
    复制代码

    2.1、解析XML

    复制代码
    from xml.etree import ElementTree as ET
    
    
    # 打开文件,读取XML内容
    str_xml = open('xo.xml', 'r').read()
    
    # 将字符串解析成xml特殊对象,root代指xml文件的根节点
    root = ET.XML(str_xml)
    
    利用ElementTree.XML将字符串解析成xml对象
    复制代码
    复制代码
    from xml.etree import ElementTree as ET
    
    # 直接解析xml文件
    tree = ET.parse("xo.xml")
    
    # 获取xml文件的根节点
    root = tree.getroot()
    
    利用ElementTree.parse将文件直接解析成xml对象
    复制代码

    2.2操作XML

     xml模块功能一览表

    2.3遍历XML文档的所有内容

     遍历XML文档内容

    2.4遍历XML中指定的节点

     遍历XML中节点内容

    3.python面向对象

    面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” 的使用。

      类就是一个模板,模板里可以包含多个函数,函数里实现一些功能

      对象则是根据模板创建的实例,通过实例对象可以执行类中的函数

    • class是关键字,表示类
    • 创建对象,类名称后加括号即可

    3.1面向对象三大特性

    面向对象的三大特性是指:封装、继承和多态。

    一、封装

    封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容。

    所以,在使用面向对象的封装特性时,需要:

    • 将内容封装到某处
    • 从某处调用被封装的内容

    第一步:将内容封装到某处

     self 是一个形式参数,当执行 obj1 = Foo('wupeiqi', 18 ) 时,self 等于 obj1

                                  当执行 obj2 = Foo('alex', 78 ) 时,self 等于 obj2

    所以,内容其实被封装到了对象 obj1 和 obj2 中,每个对象中都有 name 和 age 属性,在内存里类似于下图来保存。

    第二步:从某处调用被封装的内容

    调用被封装的内容时,有两种情况:

    • 通过对象直接调用
    • 通过self间接调用

    3.2继承

    对于面向对象的继承来说,其实就是将多个类共有的方法提取到父类中,子类仅需继承父类而不必一一实现每个方法。

    注:除了子类和父类的称谓,你可能看到过 派生类 和 基类 ,他们与子类和父类只是叫法不同而已。

    3.3总结

    • 面向对象是一种编程方式,此编程方式的实现是基于对  和 对象 的使用
    • 类 是一个模板,模板中包装了多个“函数”供使用
    • 对象,根据模板创建的实例(即:对象),实例用于调用被包装在类中的函数
    • 面向对象三大特性:封装、继承和多态
  • 相关阅读:
    与众不同 windows phone (39)
    与众不同 windows phone (38)
    与众不同 windows phone (37)
    与众不同 windows phone (36)
    与众不同 windows phone (35)
    与众不同 windows phone (34)
    重新想象 Windows 8 Store Apps 系列文章索引
    重新想象 Windows 8 Store Apps (71)
    重新想象 Windows 8 Store Apps (70)
    重新想象 Windows 8 Store Apps (69)
  • 原文地址:https://www.cnblogs.com/meng-wei-zhi/p/8120293.html
Copyright © 2011-2022 走看看