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 problem set 3.3 and are here to assess your progress/competency as a solo programmer. You should work on these individually. While working on these problems, you may refer to your notes, textbook, any programs you wrote, and my videos. However, you should significantly restrict your discussions with classmates. It is moderately acceptable to discuss how you solved a problem in general terms, but you should never show your code to a classmate, whether you are the one struggling or the one helping. You can talk about ideas, but not specific solutions.
You should not be using an AI to assist you with these assignments. Dependence on these will almost certainly come back and hurt you when you take controlled programming competency demos later in the course.
Don't forget about the concept of Rubber Duck Debugging. If you are getting stuck this week you should absolutely pull out your rubber duck and talk to it. YES, you will feel foolish at first. But, again, the research shows that by talking out loud you can often (not always, but often) figure things out on your own. TRY IT!