python第七天

Posted 北方姆Q

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python第七天相关的知识,希望对你有一定的参考价值。

上周出现了意外情况延迟了好几天,这几天又在整云平台,真是累成汪汪了,还有个叫ossim的安全管理平台,一个可以让服务器红屏的系统,北方还真是第一次见到红屏,涨姿势了啊~~~蓝屏大家肯定都已经习惯了嘛,当时以为要爆炸了。

今天早上公司又在开洗脑大会,北方还怕你不成啊?来啊,互相伤害啊!,企业文化跟中二病你觉得我会选哪个

 

1.configparser模块:以后可以对该类型的配置文件进行操作了呦

 1 #!/usr/bin /env python
 2 import configparser
 3 config = configparser.ConfigParser()
 4 config.read(\'a.txt\', encoding=\'utf-8\')
 5 ret = config.sections()                     #获取所有节点名
 6 print(ret)
 7 
 8 ret = config.options(\'test1\')               #获取指定节点下的key
 9 print(ret)
10 
11 ret = config.get(\'test1\', \'k1\')               #获取指定节点下的值
12 print(ret)
13 
14 has = config.has_section(\'test1\')               #验证是否包含节点
15 print(has)
16 
17 has = config.has_section(\'tttt1\')               #验证是否包含节点
18 print(has)
19 
20 config.add_section(\'tttt1\')                     #加节点(节点不可以存在)
21 config.write(open(\'a.txt\', \'w\', encoding=\'utf-8\'))
22 
23 config.remove_section(\'tttt1\')                  #删节点(节点存在)
24 config.write(open(\'a.txt\', \'w\', encoding=\'utf-8\'))
25 
26 has = config.has_option(\'tttt1\', \'k1\')               #验证是否包含节点内的key
27 print(has)
28 
29 config.add_option(\'tttt1\', \'k1\')                     #加节点内key(节点存在)
30 config.write(open(\'a.txt\', \'w\'))
31 
32 config.remove_option(\'tttt1\', \'k1\')                  #删节点内key(节点存在)
33 config.write(open(\'a.txt\', \'w\'))
1 [test1]
2 k1 = v1
3 k2 = 123
4 
5 [test2]
6 k1 = ace
7 
8 [test3]
9 k1 = kkk

2.xml模块:xml是一种语法,类似于网页返回字符串后用json来格式化,获取数据后继续我们的操作

1 #!/usr/bin /env python
2 from xml.etree import ElementTree as ET
3 tree = ET.parse(\'a.xml\')
4 root = tree.getroot()                           #获取根节点
5 #print(root)
6 for child in root:                              #打印子节点
7     # print(child.tag, child.attrib)
8      for cchild in child:
9          print(cchild.tag, cchild.attrib,cchild.text)       #命名,标注,值
1 C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python35\\python.exe C:/Users/Administrator/PycharmProjects/ACE/study7/s2.py
2 rank {\'updatae\': \'yes\'} 2
3 year {} 2016
4 mounth {} None
5 
6 Process finished with exit code 0
1 <data title="CEO" age="24">
2     <country name="china" age="22">
3         <rank updatae="yes">2</rank>
4         <year>2016</year>>
5         <mounth />
6     </country>
7     <country name="abc" />
8 </data>

 

 

 1 #!/usr/bin /env python
 2 from xml.etree import ElementTree as ET
 3 from xml.dom import minidom
 4 
 5 
 6 def prettify(elem):
 7     """
 8     将节点转换成字符串
 9     :param elem:节点
10     :return:字符串
11     """
12     r_string = ET.tostring(elem, encoding=\'utf-8\')
13     rep = minidom.parseString(r_string)
14     return rep.toprettyxml(indent=\'\\t\')
15 
16 
17 root = ET.Element(\'family\')                                 #根节点命名
18 
19 son1 = ET.Element(\'son\', {\'name\': \'son1\'})                  #子节点命名
20 son2 = ET.Element(\'son\', {\'name\': \'son2\'})
21 
22 if __name__ == \'__main__\':
23     grandson1 = ET.Element(\'grandson\', {\'name\': \'grandson1\'})   #孙节点命名...
24 grandson2 = ET.Element(\'grandson\', {\'name\': \'grandson2\'})
25 son1.append(grandson1)                                             #子节点添加孙节点
26 son2.append(grandson2)
27 
28 root.append(son1)                                                   #根节点添加子节点
29 root.append(son2)
30 
31 # tree = ET.ElementTree(root)                                         #将整体写入文件(一行形式)
32 # tree.write(\'b.xml\', encoding=\'utf-8\', short_empty_elements=False)
33 
34 raw_str = prettify(root)
35 
36 with open(\'b.xml\', \'w\', encoding=\'utf-8\') as f:
37     f.write(raw_str)
1 <?xml version="1.0" ?>
2 <family>
3     <son name="son1">
4         <grandson name="grandson1"/>
5     </son>
6     <son name="son2">
7         <grandson name="grandson2"/>
8     </son>
9 </family>

 3.面向对象!!!!!!!!!

 1 #!/usr/bin /env python
 2 class F1:
 3 
 4     def show(self):
 5         print(\'show\')
 6 
 7     def foo(self):
 8         print(self.name)
 9 
10 
11 class F2(F1):
12 
13     def __init__(self, name):
14         self.name = name
15 
16     def bar(self):
17         print(\'bar\')
18 
19     def show(self):
20         print(\'show 2\')
21 
22 obj = F2(\'ace\')
23 obj.foo()
1 C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python35\\python.exe C:/Users/Administrator/PycharmProjects/ACE/study7/s6.py
2 ace
3 
4 Process finished with exit code 0
 1 #!/usr/bin /env python
 2 class c1:
 3 
 4     def __init__(self, name, obj):
 5         self.name = name
 6         self.obj = obj
 7 
 8     def show(self):
 9         print(self.name)
10         print(self.obj)
11 
12 
13 class c2:
14 
15     def __init__(self, name, age):
16         self.name = name
17         self.age = age
18 
19     def show(self):
20         print(self.name)
21         print(self.age)
22         return \'meng\'
23 
24 
25 class c3:
26     def __init__(self, a1):
27         self.aaa = a1
28 
29 
30 c2_obj = c2(\'a\', \'11\')
31 c1_obj = c1(\'ace\', c2_obj)
32 print(c1_obj.obj.age)
33 c3_obj = c3(c1_obj)
34 print(c3_obj.aaa.obj.name)
35 print(c3_obj.aaa.obj.show())
1 C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python35\\python.exe C:/Users/Administrator/PycharmProjects/ACE/study7/s5.py
2 11
3 a
4 a
5 11
6 meng
7 
8 Process finished with exit code 0
 1 #!/usr/bin /env python
 2 class SQLHELP:
 3     def __init__(self, a1, a2, a3):
 4         self.hhost = a1
 5         self.uusername = a2
 6         self.ppassword = a3
 7 
 8     def fetch(self, sql):
 9         print(self.hhost)
10         print(self.uusername)
11         print(self.ppassword)
12         print(sql)
13 
14 obj1 = SQLHELP(\'localhost\', \'root\', \'redhat\')
15 obj1.fetch(\'select * from *\')

我草,看不懂啊....其实我懂,但是我不知道怎么写.....反正很nb就是了.....

 

我就不说话,我就发图

 

以上是关于python第七天的主要内容,如果未能解决你的问题,请参考以下文章

七天学会Python基础-第七天2/3-装饰器

python第七天

python学习笔记:第七天(函数)

python第七天

第七天

Python之第七天的努力--基础数据类型补充,编码进阶