您的位置:首页 > 编程语言 > Python开发

文成小盆友python-num7 -常用模块补充 ,python 牛逼的面相对象

2016-06-24 17:55 453 查看
本篇内容:

常用模块的补充

python面相对象

一。常用模块补充

1.configparser模块

configparser 用于处理特定格式的文件,起内部是调用open()来实现的,他的使用场景是操作特定格式的文件。

特定的格式如下:

#
[section1]      #节点名称
k1 = v1         #值1
k2 = v2         #值2

[section2]      #节点名称
k1 = v1          #值


获取文件中的所有节点

##configparser 模块使用
#1.获取所有的节点
import configparser

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

print(ret)

#显示如下:
['section1', 'section2']

Process finished with exit code 0


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

import configparser

config = configparser.ConfigParser()
config.read('config.txt',encoding='utf-8')
#2.获取指定节点下所有的值对

ret2 = config.items("section1")
print(ret2)

#显示如下:
[('k1', 'v1'), ('k2', 'v2')]

Process finished with exit code 0


获取指定节点下的所有键

import configparser

config = configparser.ConfigParser()
config.read('config.txt',encoding='utf-8')
#3获取指定节点下所有的键

ret3 = config.options("section1")
print(ret3)

#显示如下:
['k1', 'k2']

Process finished with exit code 0


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

import configparser

config = configparser.ConfigParser()
config.read('config.txt',encoding='utf-8')
# #4.获取指定节点下key的 值
v = config.get('section1','k1')
print(v)
#v = config.getint('name','name')      # 这种方法也可以获取到值,只是类型不同
#v = config.getfloat('name','name')
#v = config.getboolean('name','name')

#显示如下:

v1

Process finished with exit code 0


检查节点是否存在,删除节点,添加节点

import configparser

config = configparser.ConfigParser()
config.read('config.txt',encoding='utf-8')

#4.检查,删除,添加节点
#检查
has_sec = config.has_section('name')
print(has_sec)
# #添加
config.add_section("test")
config.write(open("config.txt","w",encoding='utf-8'))

#删除节点:
config.remove_section("test")
config.write(open("config.txt","w",encoding='utf-8'))


检查删除指定组内的键值对:

import configparser

config = configparser.ConfigParser()
config.read('config.txt',encoding='utf-8')

##检查,删除,设置 指定组内的键值对
#检查
has_opt = config.has_option('section1', 'k1')
print(has_opt)

# # 删除
config.remove_option('section1', 'k1')
config.write(open("config.txt","w",encoding='utf-8'))
#设置
config.set('section1', 'k10', "123")
config.write(open("config.txt","w",encoding='utf-8')


2.上档次的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>


先加载成xml对象

from xml.etree import ElementTree as ET

# 先打开文件加载
# 再将字符串解析成xml特殊对象,root代指xml文件的根节点
str_xml = open('xo.xml', 'r').read()
root = ET.XML(str_xml)


from xml.etree import ElementTree as ET
# 直接解析xml文件
tree = ET.parse("xo.xml")
# 获取xml文件的根节点
root = tree.getroot()


在操作xml对象

在mxl标签与标签之间是可以嵌套的,然而对于每个节点的操作功能是相同的,如下的功能都能使用。

class D(object):

def bar(self):
print 'D.bar'

class C(D):

def bar(self):
print 'C.bar'

class B(D):

def bar(self):
print 'B.bar'

class A(B, C):

def bar(self):
print 'A.bar'

a = A()
# 执行bar方法时
# 首先去A类中查找,如果A类中没有,则继续去B类中找,如果B类中么有,则继续去C类中找,如果C类中么有,则继续去D类中找,如果还是未找到,则报错
# 所以,查找顺序:A --> B --> C --> D
# 在上述查找bar方法的过程中,一旦找到,则寻找过程立即中断,便不会再继续找了
a.bar()

新式类多继承


新式类多继承
对于以上的说明:

经典类:首先去A类中查找,如果A类中没有,则继续去B类中找,如果B类中么有,则继续去D类中找,如果D类中么有,则继续去C类中找,如果还是未找到,则报错

新式类:首先去A类中查找,如果A类中没有,则继续去B类中找,如果B类中么有,则继续去C类中找,如果C类中么有,则继续去D类中找,如果还是未找到,则报错

注意:在上述查找过程中,一旦找到,则寻找过程立即中断,便不会再继续找了

如下两种继承关系顺序: 红线表示的是继承关系,而数字表示的是查找顺序





3.特性之 多态

Pyhon不支持多态并且也用不到多态,多态的概念是应用于Java和C#这一类强类型语言中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: