Homework Today:
1, the total number of characters in the file a.txt?
2, study questions think why the sum of n times after the first time is the result of 0?
With open (' A.txt ', encoding= ' utf-8 ') as F:
g= (Len (line) to line in F)
Print (SUM (g))
3, the file Shopping.txt content as follows
mac,2000,3
lenovo,3000,10
tesla,1000000,10
chicken,200,1
How much does it cost in total?
Print out all the product information in the form
[{' name ': ' xxx ', ' price ': ' 3333 ', ' Count ': 3},....]
Product information of more than 10000 per unit price, the format of the same
4, the file content is as follows, the title is: Name, sex, age, salary
Egon Male 18 3000
Alex Male 38 30000
Wupeiqi Female 28 20000
Yuanhao Female 28 10000
Requirements:
Remove each record from the file into the list,
Each element of the list is {' name ': ' Egon ', ' sex ': ' Male ', ' age ': the form of ' salary ': 3000}
5 based on the list obtained by 1, take out the information of the person with the highest salary
6 According to 1 get the list, take out the information of the youngest person
7 based on the list obtained in 1, the name in each person's information is mapped to the first letter capitalized form
8 according to the 1 obtained list, filter out the information of the person whose name begins with a
##1Sum_file =0with Open ('a.txt', encoding='Utf-8') as F: forLineinchF:sum_file+=Len (line)Print(Sum_file) with open ('a.txt', encoding='Utf-8') as F:g= (Len (line) forLineinchf)Print(SUM (g))##2#g is the generator, the generator is the value of the property is finished from beginning to end, the sum iteration value is taken out the first time, so the back G is 0, that is, sum is also 0##3Total =0with Open ('Shopping.txt', encoding='Utf-8') as F: forLineinchF: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]) forLineinchf)Print(SUM (g)) Shopping_info=[]with Open ('Shopping.txt', encoding='Utf-8') as F: forLineinchF:line= Line.strip ('\ n'). Split (',') Keys= ['name',' Price','Count'] D= {K:v forKvinchZip (keys,line)} shopping_info.append (d)Print(d)Print(shopping_info) Shopping_info=[]with Open ('Shopping.txt', encoding='Utf-8') as F: forLineinchF:line= Line.strip ('\ n'). Split (',') ifInt (line[1]) > 10000: Keys= ['name',' Price','Count'] D= {K:v forKvinchZip (keys,line)} shopping_info.append (d)Print(Shopping_info)##4User_info =[]with Open ('4.txt', encoding='Utf-8') as F: forLineinchF:line=line.split () keys= ['name','Sex',' Age','Salary'] D= {K:v forKvinchZip (keys,line)} user_info.append (d)Print(User_info)##5User_info =[]with Open ('4.txt', encoding='Utf-8') as F: forLineinchF:line=line.split () keys= ['name','Sex',' Age','Salary'] D= {K:v forKvinchZip (keys,line)} user_info.append (d)Print(user_info) salary_list= [Int (index['Salary']) forIndexinchUser_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: forLineinchF:ifres = = Int (Line.split () [3]): Print(line)##6User_info =[]with Open ('4.txt', encoding='Utf-8') as F: forLineinchF:line=line.split () keys= ['name','Sex',' Age','Salary'] D= {K:v forKvinchZip (keys,line)} user_info.append (d)Print(user_info) Age_info= (int (age[' Age']) forAgeinchuser_info)#For age in User_info:#Age = Int. (age[' age ')#Age_info.append (age) forInfoinchUser_info:ifInt (info[' Age']) ==min (age_info):Print(Info)##7User_info =[]with Open ('4.txt', encoding='Utf-8') as F: forLineinchF:line=line.split () keys= ['name','Sex',' Age','Salary'] D= {K:v forKvinchzip (keys, line)} user_info.append (d)Print(User_info) forNameinchuser_info:name['name'] = name['name'].capitalize ()Print(User_info)##8User_info =[]with Open ('4.txt', encoding='Utf-8') as F: forLineinchF:line=line.split () keys= ['name','Sex',' Age','Salary'] D= {K:v forKvinchzip (keys, line)} user_info.append (d)Print(user_info) New_user_info= [Aname forAnameinchUser_infoif notaname['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)
View Code
Python-code-14