Class Notes (835,107)
Canada (508,933)
CSC108H1 (113)
Lecture

november 23 notes

2 Pages
140 Views
Unlock Document

Department
Computer Science
Course
CSC108H1
Professor
Michelle Craig
Semester
Fall

Description
 Parsing a file: read groups function 1. def read_groups(f): 2. 3. groups = {} 4. 5. line = f.readline() 6. while line != ”“: 7. # while line: this also works, python idiom; 8. # b/c every string except for an empty string is true 9. 10. group_name = line[11:].strip() 11. group_members = [] 12. 13. line = f.readline() 14. while line != “” and line != „\n‟: # use while b/c doesn‟t 15. # know how many members in group 16. line = line.strip() 17. if line.startswith(„*‟): # astreix indicates group leader who goes first 18. # this also works: 19. # if line[0] == „*‟: 20. 21. member_name = line[3 : -1]: 22. group_members.insert(0, member_name) 23. 24. else: 25. member_name = line[1 : -1] 26. group_members.append(member_name) 27. 28. line = readline() 29. 30. # finished reading one group, move on to next 31. group[group_name] = group_members 32. 33. # we need to read one more line since we are now on the empty line 34. line = f.readline() 35. 36. return groups 37. 38. if _ _name_ _ == „_ _main_ _‟: 39. print read_groups(open(„survey.txt‟))  Test files: o survey.txt o Survey1.txt 1. group ID = Sesame Street 1. group ID = Sesame Street 2. ‘g0cookie’ 2. ‘g0cookie’ 3. ‘g0ernie’ 3. ‘g0ernie’ 4. * ‘g0bigbir’ 4. * ‘g0bigbir’ 5. 5. 6. group ID = Dora the Explora 6. group ID = 108 7. * ‘g0dora’ 7. ‘g0diane’ 8. ‘g0diego’ 8. ‘g0diego’ 9. 9. ‘g0michelle’ 10. group ID = Max and Ruby 10. 11. ‘g0max’
More Less

Related notes for CSC108H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit