首页 > 代码库 > python第七天
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‘) #验证是否包含节点内的key27 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 = v13 k2 = 1234 5 [test2]6 k1 = ace7 8 [test3]9 k1 = kkk
2.xml模块:xml是一种语法,类似于网页返回字符串后用json来格式化,获取数据后继续我们的操作
1 #!/usr/bin /env python2 from xml.etree import ElementTree as ET3 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.py2 rank {‘updatae‘: ‘yes‘} 23 year {} 20164 mounth {} None5 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 = name15 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.py2 ace3 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 = name17 self.age = age18 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 = a128 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.py2 113 a4 a5 116 meng7 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第七天
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。