python整理-day7

1、模块

  1、configparser

      

conf文件
[wzc]
haha = 123

[zzz]
heihei = 333

程序文件
import configparser

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


config1=configparser.ConfigParser()
config1.read('conf',encoding='utf-8')
ret1=config.items('wzc')
print(ret1)

config2=configparser.ConfigParser()
config2.read('conf',encoding='utf-8')
value=config2.options('wzc')
print(value)

config3=configparser.ConfigParser()
config3.read('conf',encoding='utf-8')

v=config3.get('wzc','haha')
v1=config3.getint('wzc','haha')
#v2=config3.getfloat()
#v3=config3.getboolean()
print(v,type(v))
print(v1,type(v1))

has_sec=config3.has_section('wzc')
print(has_sec)

config3.remove_section('zzz')
config3.write(open('conf','w'))

config3.add_section('zzz')
config3.write(open('conf','w'))

has_opt=config3.has_option('zzz','heihei')
print(has_opt)

config3.remove_option('zzz','heihei')
config3.write(open('conf','w'))

config3.set('zzz','heihei','333')
config3.write(open('conf','w'))

结果:

['wzc', 'zzz']
[('haha', '123')]
['haha']
123 <class 'str'>
123 <class 'int'>
True
False

  configparser模块需要注意的地方

    模块胡IBA数字和字符串都当做字符串来处理,如果非要输入数字,可以使用getint来进行输入,这样得到的就含有数字。

 

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模块,我们主要用到elementTree这个功能,每一个节点都是一个element对象

我们在使用的时候又两种方法,一种是直接使用element,还有一种是elementTree然后再使用element。

但是这两者之前还是有区别的,区别就是elementTree上面,为了我们把Xml配置读取以后,还是要进行写入的,如果不使用elementTree的话我们无法进行写入文件的操作

from xml.etree import ElementTree as ET

tree=ET.parse('xml')
root=tree.getroot()
#print(root)

for child in root:
    #print(child.tag,child.attrib)
    for cchild in child:
        print(cchild.tag,child.attrib)

  结果:

rank {'name': 'Liechtenstein'}
year {'name': 'Liechtenstein'}
gdppc {'name': 'Liechtenstein'}
neighbor {'name': 'Liechtenstein'}
neighbor {'name': 'Liechtenstein'}
rank {'name': 'Singapore'}
year {'name': 'Singapore'}
gdppc {'name': 'Singapore'}
neighbor {'name': 'Singapore'}
rank {'name': 'Panama'}
year {'name': 'Panama'}
gdppc {'name': 'Panama'}
neighbor {'name': 'Panama'}
neighbor {'name': 'Panama'}

 

newxml.xml

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year age="16">2024</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year age="16">2027</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year age="16">2027</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

  

 

from xml.etree import ElementTree as ET

set_xml=open('xml').read()
root=ET.XML(set_xml)
print(root)

for node in root.iter('year'):
    print(node)
    new_year=int(node.text) + 1
    node.text=str(new_year)

    node.set('name','wzc')
    node.set('age','16')

    del node.attrib['name']
tree=ET.ElementTree(root)
tree.write('newxml.xml',encoding='utf-8')

  结果:

<Element 'data' at 0x101121138>
<Element 'year' at 0x10172f048>
<Element 'year' at 0x10172f228>
<Element 'year' at 0x10172f3b8>

  

from xml.etree import ElementTree as ET

root=ET.Element('home')

son1=ET.Element('son',{'name':'son1'})
son2=ET.Element('son',{'name':'son2'})

grandson1=ET.Element('grandson',{'name':'grandson1'})
grandson2=ET.Element('grandson',{'name':'grandson2'})

son1.append(grandson1)
son2.append(grandson2)

root.append(son1)
root.append(son2)

tree=ET.ElementTree(root)
tree.write('xx.xml',encoding='utf-8',xml_declaration=True,short_empty_elements=False)

  结果:

<?xml version='1.0' encoding='utf-8'?>
<home><son name="son1"><grandson name="grandson1"></grandson></son><son name="son2"><grandson name="grandson2"></grandson></son></home>

  如果要换行的话,需要加几条代码

def prettify(elem):
    """将节点转换成字符串,并添加缩进。
    """
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")

 

zipfile和tarfile模块,这两个模块是压缩解压用的

import zipfile
z=zipfile.ZipFile('123.zip','w')
#z=zipfile.ZipFile('123.zip','a')

z.write('conf')
zipfile.ZipFile('123.zip','r')
z.extractall()
z.close()

import tarfile
tar=tarfile.open('222.tar','w')
tar.add('1-1.py',arcname='s1.py')
tar.add('2-1.py')
tar.close()

tar=tarfile.open('222.tar','r')
tar.extractall()
tar.close()

  

subprocess模块

通过这个模块可以执行shell命令,同时这个有一个popen功能,可以尝试用这个模块进行expect的功能

  

import subprocess

ret=subprocess.call(["ls","-l"],shell=False)
ret1=subprocess.call("ls -l",shell=True)

obj=subprocess.Popen(["su"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)

  结果:

total 104
-rw-r--r--  1 wangzhichao  staff  946 Jun 19 10:33 1-1.py
-rw-r--r--  1 wangzhichao  staff  213 Jun 23 18:40 2-1.py
-rw-r--r--  1 wangzhichao  staff  358 Jun 19 11:52 3-1.py
-rw-r--r--  1 wangzhichao  staff  449 Jun 19 14:54 4-1.py
-rw-r--r--  1 wangzhichao  staff  328 Jun 19 15:05 5-1.py
-rw-r--r--  1 wangzhichao  staff  227 Jun 19 15:48 6-1.py
-rw-r--r--  1 wangzhichao  staff  365 Jun 19 17:09 7-1.py
-rw-r--r--  1 wangzhichao  staff  415 Jun 19 18:09 8-1.py
-rw-r--r--  1 wangzhichao  staff  338 Jun 19 18:28 9-1.py
-rw-r--r--  1 wangzhichao  staff   44 Jun 23 18:16 conf
-rw-r--r--  1 wangzhichao  staff  717 Jun 24 10:46 newxml.xml
-rw-r--r--  1 wangzhichao  staff  690 Jun 19 11:12 xml
-rw-r--r--  1 wangzhichao  staff  174 Jun 24 12:13 xx.xml

  

 

python是支持函数式+面向对象式编程

OOP:(面向对象)

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

 类里面必须要在函数加一个self

面向对象:类,对象

a。创建类

  class 类名:

    def 方法名(self,xxx):

      pass

b。创建对象

  对象=类名()

c。通过对象执行方法

  对象.方法名(123)

什么时候用面向对象,当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可

 

self 是什么?

  self是一个Python自动会给传值的参数

  哪个对象的执行方法,self就是谁

  obj.fetch('select')。。。。。self=obj

构造方法

  类中有一个特殊的方法,__init__,类()自动被执行

class SQLhelp:
    def __init__(self,host,user,pwd):
        self.ho=host
        self.us=user
        self.pw=pwd
    def fetch(self,sql):
        print(self.ho,self.us, self.pw)
        pass
    def create(self,sql):
        pass
    def remove(self,nid):
        pass


host1="10.11.1.1"
user1='wzc'
pwd1='8888'

obj=SQLhelp(host1,user1,pwd1)
obj.fetch("wahhhh")

  结果:

10.11.1.1 wzc 8888

  

Python可以有多继承,其他余元只有单继承

父类、子类,基类、派生类

优先级是自己里面的最高,所以在寻找方法的时候,优先找自己的

class c1:
    def __init__(self,name,obj):
        self.name=name
        self.obj=obj

class c2:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def show(self):
        print(self.name)
class c3:
    def __init__(self,name):
        self.money=11
        self.aaa=name

c2_obj=c2('wzc',18)
c1_obj=c1('www',c2_obj)
c3_obj=c3(c1_obj)


#print(c1_obj.obj.show())
c3_obj.aaa.obj.show()

  结果

wzc

  

继承关系:

  优先级根据继承顺序,左边优先级高

所以这个寻找方法最后总结就是两种方式,分别是深度优先和广度优先

posted @ 2016-06-20 11:17  wzc1206  阅读(178)  评论(0编辑  收藏  举报