dic={'001':{'name':'张三','age':18,'num':'001'},
'002':{'name':'张二','age':28,'num':'002'}}
class FileAction():
@staticmethod
def writeDate(dic):
f=open('data.txt','w')
for k,v in dic.items():
for k1,v1 in v.items():
f.write(k1+':'+str(v1))
if k1=='num':
f.write('\n')
else:
f.write(',')
f.close()
@staticmethod
def readData():
f=open('data.txt','r')
line=f.readline()
zd_wai={}
while len(line)>0:
items=line.strip().split(',')
zd_nei={}
for item in items: # 这个循环,把 键:值 形式的数据分成 键和值。检查键是否是age,是就把值int化。检查键是否是num,
s=item.split(':')
if s[0]=='age':
zd_nei[s[0]]=int(s[1])
else:
zd_nei[s[0]]=s[1]
if s[0]=='num': # 这两行在for里面和外面一个效果,因为 s[0]=='num'时,这个for循环也执行完了,zd_nei刚好成了一个完整的字典。
zd_wai[s[1]]=zd_nei # 这个字典里面,外层字典的key恰好和内层字典num的值一样,所以这样用
line=f.readline()
print(zd_wai)
f.close()
# FileAction.writeDate(dic)
FileAction.readData()
#************************************************
dic={'001':{'name':'张三','age':18,'num':'001'},
'002':{'name':'张二','age':28,'num':'002'}}
class FileAction():
@staticmethod
def writeDate(dic):
f=open('data.txt','w')
for k1,v1 in dic.items():
for k2,v2 in v1.items():
f.write(k2+':'+str(v2)) # 'age'的int值要转成str
if k2=='num': # 写入后检查,如果刚刚写的是num键值,就换行,不是就加逗号隔开键值对
f.write('\n')
else:
f.write(',')
f.close()
@staticmethod
def readData():
f=open('data.txt','r')
line=f.readline()
zd_wai={}
while len(line)>0:
items=line.strip().split(',')
zd_nei={}
for item in items:
item_buff=item.split(':')
zd_nei[item_buff[0]]=int(item_buff[1]) if item_buff[0]=='age' else item_buff[1]
# if item_buff[0]=='age':
# zd_nei[item_buff[0]]=int(item_buff[1])
# else
# zd_nei[item_buff[0]]=item_buff[1]
if item_buff[0]=='num':
zd_wai[item_buff[1]]=zd_nei
line=f.readline()
print(zd_wai)
f.close()
FileAction.writeDate(dic)
FileAction.readData()
#**********************************************************
dic={'001':{'name':'张三','age':18,'num':'001'},
'002':{'name':'张二','age':28,'num':'002'}}
class FileAction():
@staticmethod
def writeData(dic):
f=open('data.txt','w')
for k1,v1 in dic.items():
for k2,v2 in v1.items():
f.write(k2+':'+str(v2))
if k2=='num':
f.write('\n')
else:
f.write(',')
f.close()
@staticmethod
def readData():
f=open('data.txt','r')
line=f.readline()
zd_wai={}
while len(line)>0:
zd_nei={}
items_buff=line.strip().split(',')
for item_buff in items_buff:
item=item_buff.split(':')
zd_nei[item[0]]=int(item[1]) if item[0]=='age' else item[1]
# if item[0]=='age':
# zd_nei[item[0]]=int(item[1])
# else:
# zd_nei[item[0]]=item[1]
if item[0]=='num':
zd_wai[item[1]]=zd_nei
line=f.readline()
print(zd_wai)
f.close()
FileAction.writeData(dic)
FileAction.readData()
#****************把下面字典的内层字典,按照k:v 的形式写入data.txt,读取data.txt内容 并恢复、打印出dic。*******************************************
dic={'001':{'name':'张三','age':18,'num':'001'},
'002':{'name':'张二','age':28,'num':'002'}}
class FileAction():
@staticmethod
def writeData(dic):
f=open('data.txt','w')
for k1,v1 in dic.items():
for k2,v2 in v1.items():
f.write(k2+':'+str(v2))
f.write('\n') if k2=='num' else f.write(',')
f.close()
@staticmethod
def readData():
f=open('data.txt','r')
line=f.readline()
zd_wai={}
while len(line)>0:
zd_nei={}
items_buff=line.strip().split(',')
for item_buff in items_buff:
item=item_buff.split(':')
zd_nei[item[0]]=int(item[1]) if item[0]=='age' else item[1]
if item[0]=='num':
zd_wai[item[1]]=zd_nei
line=f.readline()
print(zd_wai)
f.close()
FileAction.writeData(dic)
FileAction.readData()
#***************************************************
dic={'001':{'name':'张三','age':18,'num':'001'},
'002':{'name':'张二','age':28,'num':'002'}}
class FileAction():
@staticmethod
def writeData(dic):
f=open('data.txt','w')
for k1,v1 in dic.items():
for k2,v2 in v1.items():
f.write(k2+':'+str(v2))
f.write('\n') if k2=='num' else f.write(',')
f.close()
@staticmethod
def readData():
f=open('data.txt','r')
line=f.readline()
zd_wai={}
while len(line)>0:
zd_nei={}
items_buff=line.strip().split(',')
for item_buff in items_buff:
item=item_buff.split(':')
zd_nei[item[0]]=int(item[1]) if item[0]=='age' else item[1]
if item[0]=='num':
zd_wai[item[1]]=zd_nei
line=f.readline()
print(zd_wai)
f.close()
FileAction.writeData(dic)
FileAction.readData()
#**********上面易错点:f.write写成print。写入txt的时候要str化。读出来放入字典时要int化的值。f.readline()少写f.****************************************************
a=7
a=a-3 if a>8 else 2 # a=((a-3) if a>8 else 2)
print(a) # 2
a=7
a-=3 if a>8 else 2 # 不一定是右边这个判断情况a=a-(3 if a>8 else 2)
print(a) # 5
#**********反正就是,-= +=在三元表达式中可能出歧义。所以不要用。*************************
class People():
tax=0
def __init__(self,name,age,work,salary):
self.name=name
self.age=age
self.work=work
self.salary=salary
self.__energy=100
def eat(self,money):
self.__energy=self.__energy+10 if self.__energy<90 else 100
self.salary-=money
def working(self):
People.tax+=self.salary*0.2
self.__energy=self.__energy-30 if self.__energy>30 else 0
def sleep(self):
self.__energy=self.__energy+60 if self.__energy<40 else 100
def say(self):
print(self.name,self.age,self.__energy)
def get_energy(self):
return self.__energy
class Woman(People):
def shopping(self,money):
self.salary-=money
class Man(People):
def say(self):
super().say()
print('笑话')
zhangsan=Man('张三',18,'开发',18)
zhangsan.say()
lisi=Woman('李四',19,'开发',8000)
lisi.shopping(9000)
lisi.say()
#*********下面是打印三角形星星***************************************
class Paint():
def paintStar(self):
for line in range(1,6):
for j in range(5-line):
print(' ',end='')
for j in range(2*line-1):
print('*',end='') if(line==5 or j==0 or j==2*line-2) else print(' ',end='')
print()
p=Paint()
p.paintStar()
#*********下面是一个学生信息管理系统。把字典写入文件,从文件读出字典。对文件中字典进行增删改查**************************************************
a={ '001':{'name':'张三','age':18,'address':'北京'},
'002':{'name':'张二','age':28,'address':'上海'},
'003':{'name':'张大','age':38,'address':'北京'},
'004':{'name':'张四','age':28,'address':'上海'},
'005':{'name':'张六','age':38,'address':'北京'}
}
class StudentAdmin():
@staticmethod
def writeData():
f=open('StudentAdmin.txt','w')
f.write(str(a))
f.close()
@staticmethod
def readData():
f=open('StudentAdmin.txt','r')
content=f.read()
print(eval(content))
f.close()
return eval(content)
StudentAdmin.writeData()
a=StudentAdmin.readData( )
while True:
caozuo=int(input("请输入操作编号:"))
if caozuo==1:
print("增")
xh=input('增加学号:')
if xh in a.keys():
print('该学号已存在')
continue
else:
zd={}
zd['name']=input('姓名')
zd['age']=int(input('年龄'))
zd['address']=input('地址')
a[xh]=zd
print(a)
elif caozuo==2:
print('删')
xh=input("要删除的学号是")
if xh in a.keys():
del a[xh]
print(a)
else:
print('改学号不存在')
continue
elif caozuo==3:
print('改')
xh=input('请输入要改的学号')
if xh in a.keys():
a[xh]['name']=input('姓名')
a[xh]['age']=int(input('年龄'))
a[xh]['address']=input('地址')
print(a)
else:
print('查无此人')
continue
elif caozuo==4:
print('查')
xh=input('要查的学号是')
if xh in a.keys():
print(xh,':',a[xh])
else:
print('查无此人')
elif caozuo==0:
print('关机')
StudentAdmin.writeData()
exit(0)
else:
print("输入错误,请重新输入")
continue
class Num():
def __init__(self,n):
self.n=n
def __call__(self, *args, **kwargs):
result=self.n
for a in args:
result *= a
for k,v in kwargs.items():
result+=v
return result
a=Num(5)
b=Num(2)
c=a(1,2,3,age=4,shengao=5)
print(c)
class Car():
def __new__(cls, *args, **kwargs): # 制造一个新对象,生产一辆车
if not hasattr(Car,'inst'):
Car.inst = object.__new__(Car)# 建立一个Car对象。给inst属性。
return Car.inst # python中所有类都在object类中
def __init__(self,name,cid):
print('你好')
self.name=name
self.cid=cid
# bmw=Car('宝马','京A66666')
a=Car('宝马','京A66666')
b=Car('奔驰','京B12345')
print(Car.inst)
# print(a is b)
# print(a.name,a.cid)
# print(b.name,b.cid)
print(a)
print(b)
# 单例模式,这个类只有一个对象。
class A():
count=0
def __init__(self,name):
self.name=name
A.count+=1
print('初始化',self.name,'计数是',A.count)
def __del__(self):
A.count-=1
print('删除',self.name,'还剩%d个'%A.count)
zs=A('张三')
ls=A('李四')
del zs #注释掉这两行,输出结果一样。那么哪些“__abcd__”是在创建实例时就默认要进入的,哪些不进?在下一个程序探索
del ls
#*************************************************
"""
class A():
count=0
def __init__(self,name):
self.name=name
A.count+=1
print('初始化',self.name,'计数是',A.count)
def __del__(self):
A.count-=1
print('删除',self.name,'还剩%d个'%A.count)
def __new__(cls, *args, **kwargs):
print("进入__new__")
def __call__(self, *args, **kwargs):
print("进入__call__")
def __add__(self, other):
print('进入__add__')
def __aexit__(self, exc_type, exc_val, exc_tb):
print('进入__aexit__')
def __prepare__(metacls, name, bases):
print('进入__prepare__')
zs=A('张三')
ls=A('李四')
#************下面一个,是用 __new__方法 实现 单例模式。使Car这个类只有一个对象,也就是说,创建多个对象,都是一个对象,内存值一样。*************************************************
#
class Car():
def __new__(cls, *args, **kwargs):
if not hasattr(Car,'inst'):
Car.inst=object.__new__(Car)
return Car.inst
def __init__(self,name,cid):
print('你好')
self.name=name
self.carid=cid
a=Car('宝马','京A66666')
b=Car('奔驰','京B12345')
print(a is b)
print(a.name,a.carid)
print(b.name,b.carid)
print(a)
print(b)
#
# class Car():
# def
#
# import time
# a=[1,2,3,4,5]
# while True:
# time.sleep(2)
# t=a[0]
# for j in range(len(a)-1):
# a[j]=a[j+1]
# a[len(a)-1]=t
# print(a)
(学生管理系统)(+= -=不要用在三元运算中)(单例模式)
猜你喜欢
转载自blog.csdn.net/weixin_44675370/article/details/88540154
今日推荐
周排行