Week 13
File it under AWESOME
Weekly Summary
In my mind, most of what we have done with Python so far is absolutely, 100% necessary. But it is also often kind of boring! It isn't until this week that we really start to do some cool things. This week we want to learn how to use simple text files - well formatted files of simple ASCII characters - to provide our programs with access to "large" amounts of data. Once we know how to read and write from files we have the ability to really do some fascinating things.
Learning Outcomes
By the end of the week students should be able to:
- Explain and/or properly use the following functions that allow us to work with text files:
- open()
- close()
- split()
- readline()
- write()
- Explain the purpose of
- myString[0:-1] (or myString[:-1] which does the same thing.)
- Reading Code
- Given a snippet of code containing one or more of the new functions listed above, indicate the results of the code.
- Writing Code
- Given a problem statement that includes the name of a function and the parameters required by that function, as well as the description of a text data file to go with it, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- Debugging Code
- Given a problem statement and a non-working Python function that attempts to solve the problem, identify where the function is incorrect and explain how to fix it.
Learning Materials
Textbook Readings
- Log on to your online textbook
- Read the following sections from your textbook
- 8.1. Persistence
- 8.2. Opening files
- 8.3. Text files and lines
- 8.4. Reading files
- 8.5. Searching through a file
- 8.6. Letting the user choose the file name
- 8.7. Using try, except, and open - This is good material, but we won't focus on this in our limited time left.
- 8.8. Writing files
- 8.9. Debugging
Lecture Videos
- Reading from Files - Miss Tea Customer Database
- Large File - MissTeaCustomerData.csv
- Small File - SmallSample.csv
- Getting started understanding reading from files
- Producing a function to count our customers from a single state
- VIDEO: Quick explanation (for those who want to try to do this before I do)
- VIDEO: stateCount()
- VIDEO: defining the encoding type
- Producing a function that tells me the average order amount for any customer
- Producing a function that tells me the average order amount for the customers from a given state
- VIDEO: Quick explanation (for those who want to try to do this before I do)
- VIDEO: averageSaleByState() solution
- Determine the average sales for EVERY state
- VIDEO: Explanation
- Writing to Files - Olympic Athlete Fitness Data
- Large File - athletes_data.csv
- Small File - athletes_sample.csv
- VIDEO: Olympic Athletes - Explaining the data
- VIDEO: How do we write to a file?
- VIDEO: Olympic Athletes - addBMI()
Practice Materials
Paired Programming Activity
Please complete the following Paired Programming activity with your assigned partner for the week.
Additional Textbook Activities
These are great for some additional practice. You should complete these on your own rather than with a partner.
Code Walkthroughs
The following are videos and code solutions for the programs discussed in the Paired Programming Activity and Individual Program Activity. You SHOULD NOT view these until you have either solved the activities yourself - and you want to see how I would do it - or you have tried at least two different times and still are stumped. Don't jump to reading the "correct answer" too quickly. I think it is a GOOD thing to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice. You need to TRY before jumping to the solutions.
Graded Materials
The following programs are formally graded as part of the Unit 3 "Problem Set." You should work on these only as an individual. While working on these problems you may refer to your notes, your textbook, any programs you wrote, and even my videos. However, you should limit your discussion of these programs with classmates. It is acceptable to discuss how you solved a problem in grand scheme of things, but you should never show your code to a classmate either as the person who is struggling or as the person who is helping a struggling classmate. You can talk ideas, but not specific solutions.
- Graded File I/O problem set (Part 3.3 of Problem Set #3).