今日作业: 1、求文件a.txt中总共包含的字符个数? 2、思考题 思考为何在第一次之后的n次sum求和得到的结果为0? with open('a.txt',encoding='utf-8') as f: g=(len(line) for line in f) print(sum(g)) 3、文件shopping.txt内容如下 mac,2000,3 lenovo,3000,10 tesla,1000000,10 chicken,200,1 求总共花了多少钱? 打印出所有的商品信息,格式为 [{'name':'xxx','price':'3333','count':3},....] 求单价大于10000的商品信息,格式同上 4、文件内容如下,标题为:姓名,性别,年纪,薪资 egon male 18 3000 alex male 38 30000 wupeiqi female 28 20000 yuanhao female 28 10000 要求: 从文件中取出每一条记录放入列表中, 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式 5 根据1得到的列表,取出薪资最高的人的信息 6 根据1得到的列表,取出最年轻的人的信息 7 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式 8 根据1得到的列表,过滤掉名字以a开头的人的信息
##1sum_file = 0with open('a.txt',encoding='utf-8') as f: for line in f: sum_file += len(line)print(sum_file)with open('a.txt',encoding='utf-8') as f: g=(len(line) for line in f) print(sum(g))##2#g是生成器,生成器的取值特性就是从头到尾取完就结束,sum迭代取值第一次就取完了,所以后面g都是0,即sum也都是0##3total =0with open('shopping.txt',encoding='utf-8') as f: for line in f: line = line.strip('\n').split(',') price = int(line[1])*int(line[2]) total += priceprint(total)with open('shopping.txt',encoding='utf-8') as f: g = (int(line.strip('\n').split(',')[1])*int(line.strip('\n').split(',')[2]) for line in f) print(sum(g))shopping_info = []with open('shopping.txt',encoding='utf-8') as f: for line in f: line = line.strip('\n').split(',') keys = ['name','price','count'] d = {k:v for k,v in zip(keys,line)} shopping_info.append(d) print(d)print(shopping_info)shopping_info = []with open('shopping.txt',encoding='utf-8') as f: for line in f: line = line.strip('\n').split(',') if int(line[1]) > 10000: keys = ['name','price','count'] d = {k:v for k,v in zip(keys,line)} shopping_info.append(d)print(shopping_info)##4user_info = []with open('4.txt',encoding='utf-8') as f: for line in f: line = line.split() keys = ['name','sex','age','salary'] d = {k:v for k,v in zip(keys,line)} user_info.append(d)print(user_info)##5user_info = []with open('4.txt',encoding='utf-8') as f: for line in f: line = line.split() keys = ['name','sex','age','salary'] d = {k:v for k,v in zip(keys,line)} user_info.append(d)print(user_info)salary_list = [int(index['salary']) for index in user_info]# for index in user_info:# salary_list.append(int(index['salary']))res = max(salary_list)with open('4.txt',encoding='utf-8') as f: for line in f: if res == int(line.split()[3]): print(line)##6user_info = []with open('4.txt',encoding='utf-8') as f: for line in f: line = line.split() keys = ['name','sex','age','salary'] d = {k:v for k,v in zip(keys,line)} user_info.append(d)print(user_info)age_info = (int(age['age']) for age in user_info)# for age in user_info:# age = int(age['age'])# age_info.append(age)for info in user_info: if int(info['age']) == min(age_info): print(info)##7user_info = []with open('4.txt', encoding='utf-8') as f: for line in f: line = line.split() keys = ['name', 'sex', 'age', 'salary'] d = {k: v for k, v in zip(keys, line)} user_info.append(d)print(user_info)for name in user_info: name['name'] = name['name'].capitalize()print(user_info)##8user_info = []with open('4.txt', encoding='utf-8') as f: for line in f: line = line.split() keys = ['name', 'sex', 'age', 'salary'] d = {k: v for k, v in zip(keys, line)} user_info.append(d)print(user_info)new_user_info = [aname for aname in user_info if not aname['name'].startswith('a')]# new_user_info = []# for aname in user_info:# if not aname['name'].startswith('a'):# new_user_info.append(aname)print(new_user_info)