# -*- coding: gb2312 -*-
class studentApp:
def __init__(self, user):
self.inputNum=0
self.interface = user
def run(self):
while True:
number,sNo,sName,sAge,sScore,sPhone,sEmail= self.interface.getSInfo()
student_info=read_info()
#print(student_info)
sinfo = {"sno":sNo,"name":sName,"age":sAge,"score":sScore,"phone":sPhone,"Email":sEmail}
#print(sinfo)
if number == '1':
student_info=read_info()
elif number == '2':
print('---Students---',student_info)
self.interface.showChart(student_info)
elif number == '3':
mod_info(sinfo,student_info)
elif number == '4':
save_info(student_info)
else:
break
self.interface.show_student_info(student_info)
self.interface.number.set('0')
def read_info():
old_info = []
try:
students_txt = open("grade.txt")
except:
print("No information")
return
while True:
info = students_txt.readline()
if not info:
break
info = info.rstrip()
info = info[1:-1]
student_dict = {}
for x in info.split(","):
# print(x)
key_value = []
for k in x.split(":"):
k = k.strip()
# print(k)
if k[0] == k[-1] and len(k) > 2:
key_value.append(k[1:-1])
else:
key_value.append(k)
# print(key_value)
student_dict[key_value[0]] = key_value[1]
# print(student_dict)
old_info.append(student_dict)
students_txt.close()
return old_info
def mod_info(sinfo,student_info):
mod_sno = sinfo.get("sno")
# print(mod_sno)
for info in student_info:
if mod_sno == info.get("sno"):
# print(mod_sno)
sScore = int(sinfo.get("score"))
#print(sScore)
info = {"sno":mod_sno,"name":info.get("name"),"age":info.get("age"),"score":sinfo.get("score"),"phone":info.get("phone"),"Email":info.get("Email")}
#print(info)
return info
def save_info(student_info):
try:
students_txt = open("grade.txt", "w")
except Exception as e:
students_txt = open("grade.txt", "x")
for a in student_info:
students_txt.write(str(a) + "\n")
print("----"+str(a))
students_txt.close()
这是我的一个代码,我希望mod_info中的info被save_info中的a得到,该如何调用info,求解?注python版本2.7