文件读写 文本文件写 1 2 3 4 5 6 7 8 9 10 with open ("D:\\wtest.txt" , mode='w' , encoding='UTF-8' ) as f : f.write("ccccccccccccccc\n" ) f.write("ccccccccccccccc\n" ) f=open ("file.txt" , "w" ) f.write("Hello, World!" ) f.close()
文本文件读 1 2 3 4 with open ("/root/python/test.txt" , mode='r' , encoding='UTF-8' ) as f: res1 = f.read() res2 = f.readlines() print (res1)
mode
w 覆盖写 a 追加写 r 读
python 库 标准库,扩展库,自定义库 ⽤ import 或者 from … import 来导⼊相应的库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import csv data = [{"id" : 1 , "passwd" : "123456" , "bat" : 1000 }, {"id" : 2 , "passwd" : "abcdef" , "bat" : 2000 }] with open ("/root/python/user.csv" , mode="w" , encoding="UTF-8" ) as f: writer = csv.DictWriter(f, fieldnames=["id" , "passwd" , "bat" ]) writer.writeheader() writer.writerows(data) with open ("/root/python/user.csv" , mode="r" , encoding="UTF-8" ) as f: reader = csv.DictReader(f) for i in reader: print (i) data.append(i)
异常 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 try : x = int (input ("请输入一个数字: " )) z=100 /x if x<0 : raise Exception("x不能小于0" ) if x>100 : raise Exception("x不能大于100" ) print ("ok" ) except ValueError: print ("您输入的不是数字,请再次尝试输入!" ) except ZeroDivisionError: print ("除数不能为零" ) except Exception as e: print ("其他错误:" ,e)
封装、继承、多态 封装 类及对象包含属性和⽅法 属性:静态特征 全局变量 成员 ⽅法:动态特征 函数 功能 魔法⽅法:不需要调⽤就可以⾃动执⾏。 作⽤:初始化对象的成员 ( 给对象添加属性 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class People : name="" age=0 def __init__ (self,xingming,nianling ): self .name=xingming self .age=nianling def show (self ): print (f"姓名是{self.name} ,年龄是{self.age} " ) if __name__=="__main__" : ldh=People("刘德华" ,50 ) ldh.show()
继承 class ⼦类名 ( ⽗类名 ): ⼦类直接具备⽗类的属性和⽅法 解决代码重⽤问题,提⾼开发效率
1 2 3 4 5 6 7 8 9 10 11 class Student (People ): grade="" def __init__ (self, xingming, nianling,nianji ): super ().__init__(xingming,nianling) self .grade=nianji def test (self ): print (f"年级是{self.grade} " ) def show (self ): print (f"姓名是{self.name} ,年龄是{self.age} ,年级是{self.grade} " )
多态 多态从字⾯上理解就是⼀个事物可以呈现多种状态。 没有继承就没有多态。 多态是能⾃⼰进⾏判断该去执⾏什么 , 创建⼀个列表来体现 , ⾯向对象的列表。
1 2 3 4 5 list1=[ldh,zjl] for i in l1: print (i.name) i.show()
练习 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 with open ("/root/python/邀请函.txt" ,mode='a' ,encoding='UTF-8' ) as f : f.write("诚挚邀请您来参加本次宴会" ) with open ("/root/python/邀请函.txt" ,mode="r" ,encoding="UTF-8" ) as f: result=f.read() print (result) def init (): try : with open ("/root/python/user.csv" ,mode="r" ) as f: result=csv.DictReader(f) data.clear() for i in result: data.append(i) except FileNotFoundError: print ("没有文件使用原数据" ) def CunKuan (userdata ): money=input ("请输入存款金额:" ) money=checkMoney(money) userdata["bat" ]=str (float (userdata["bat" ])+money) print (f"您已成功存款{money} ,您当前的余额为:{userdata['bat' ]} " ) inputToReturn() def checkMoney (m ): try : m=float (m) if m <=0 or m%100 !=0 : raise Exception("金额必须为100倍数的正整数!" ) return m except ValueError: print ("请输入整数!" ) inputToReturn() except Exception as e: print (e) inputToReturn() class Fruit : name="" color="" def __init__ (self,name,color ): self .name=name self .color=color def show (self ): print (f"{self.color} 的{self.name} 真好吃" ) if __name__=="__main__" : apple=Fruit("苹果" ,"红色" ) orange=Fruit("橘子" ,"橙色" ) watermelon=Fruit("西瓜" ,"绿色" ) apple.show() orange.show() watermelon.show() class Animal : color="" breed="" def __init__ (self,color,breed ): self .color=color self .breed=breed def eat (self ): print (f"有一只{self.color} 的{self.breed} 正在吃饭...." ) class Cat (Animal ): love="" def __init__ (self, color, breed,love ): super ().__init__(color, breed) self .love=love def eat (self ): print (f"有一只亲和度是{self.love} 级{self.color} 的{self.breed} 正在吃鱼...." ) class Dog (Animal ): loyal="" def __init__ (self, color, breed,loyal ): super ().__init__(color, breed) self .loyal=loyal def eat (self ): print (f"有一只忠诚度是{self.loyal} 级{self.color} 的{self.breed} 正在啃骨头" ) if __name__=="__main__" : cat1=Cat("花色" ,"波斯猫" ,"10" ) dog1=Dog("黑色" ,"藏獒" ,"9" ) cat2=Cat("白色" ,"加菲猫" ,"8" ) dog2=Dog("棕色" ,"茶杯犬" ,"6" ) list1=[cat1,dog1,cat2,dog2] for i in list1: i.eat()