知识:next()不会冻结函数,但是会冻结生成器,next是调用生成器,yield是写在生成器里的冻结,yield是生成器冻结的冻结。练习了文件内容转字典,字典内容写入函数
# encoding = 'UTF-8'
# 删除信息和第4题只操作一遍,所以不计数n了
def file_to_D(): # 把文件内容变成以索引为key的字典,每个索引指向一条个人信息
global D
D={}
f = open('E:test.txt','r')
d = f.readlines()
for i in d:
i = i.split(',')
i[0] = int(i[0])
D[i[0]] = i
print(D)
# print(len(D))
def save_to_file():
f = open('E:test.txt','w')
for i in D.values():
i = ','.join(i)
f.write(i)
f.close()
def question1_1():
l1 = []
n = 0
for i in D.values():
if int(i[2]) > 22:
l1.append((i[1],i[2]))
n += 1
print(l1)
print('记录: ',n)
def question1_2():
l2 = []
n = 0
for i in D.values():
if i[4] == 'IT':
l2.append(i)
n += 1
print(l2)
print('记录: ', n)
def question1_3():
l3 = []
n = 0
for i in D.values():
if not i[5].find('2013'): # 为什么这里找到2013返回的是false,百度上都说返回索引值
l3.append(i)
n += 1
print(l3)
print('记录: ', n)
def question2():
global n2
l4 = {}
n2 = 1
name = input('name:>>')
age = input('age:>>')
phone = input('phone:>>')
dept = input('dept:>>')
birth = input('birthday:>>')
staff_id = next(a) # next 冻结生成器,不是冻结函数,yield才会
l4[phone] = {staff_id,name,age,phone,dept,birth}
choice = input('还需要添加按Y,不需要按N')
if choice == 'Y':
question2()
n2 += 1
elif choice == 'N':
print('exit')
else:
ppp = input('你输错了,下次还要继续输错么? 输入N')
pp = True
def circle():
while pp:
ppp = input('你输错了,下次还要继续输错么? 输入N')
if ppp == 'N':
raise # 有什么方法让递归函数立即停止并且不报错?
else:
circle()
circle()
def question3(id):
id = int(id)
if 0 < id < len(D):
D.pop(id)
else:
print('输错了')
print(D)
save_to_file()
def question4(name='handsome', age = '666', dept= 'super man', birth = '1'): # 默认了这几个参数不让输入
for index,i in D.items():
if dept == 'IT':
i[4] = 'market'
print('修改部门后的:',i)
if name == 'Alex Li':
i[2] = '25'
print('修改年龄后的:',i)
D[index] = i
print('总表:', D)
save_to_file()
file_to_D()
a = (i for i in range(1, 1000000001))
# question1_1() # 第一题1小问
#
# question1_2() # 2小问
#
# question1_3() # 3小问
#
# question2() # 第二题
#
# print('记录: ',n2) # 第二题
# save_to_file() # 第二题
#
# question3(3) # 第三题
#
# question4(dept='IT', name='Alex Li') # 第四题