Python File Processing and Data Analysis

Verified

Added on  2019/09/13

|1
|557
|562
Homework Assignment
AI Summary
This document outlines two Python programming assignments. The first assignment requires reading a text file, extracting unique words, sorting them alphabetically, and handling potential file opening errors. The second assignment involves analyzing an email log file ('mbox.txt') to count the occurrences of each day of the week from the 'From' lines. Students are required to submit their Python code files, named with their name and the assignment title, along with a Word document containing screenshots of various testing conditions, including error handling. The assignments emphasize good programming practices, including adequate commenting and thorough testing.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Assignment #1
Write a program to open the file romeo.txt and read it line by line. For each line, split the line into a list
of words using the split function.
For each word, check to see if the word is already in a list. If the word is not in the list, add it to the list.
When the program completes, sort and print the resulting words in alphabetical order.
HINT:
1. Download the text file: romeo.txt from http://www.pythonlearn.com/code3/romeo.txt to your
local machine. For ease, place this file in the same folder as the .py file for this assignment.
2. Create and initialize a list to collect unique words.
3. Prompt the user for the file name. Use a try-except block to exit with a user-friendly error
message if there is an error opening the file name specified.
4. Once the file is open, use an iterative loop (e.g. “for” or “while”) to traverse each line of the file.
5. Use the “split” function to split the line into individual words and collect these in a list. Note –
remember to check for the condition that the line may be empty, i.e. not contain any words!
6. Check each word against our collection of unique words. If it does not exist, add it in.
7. When the program has finished traversing each line in the specified file, sort the complete
collection of unique words in alphabetical order by apply the “sort” function.
8. Remember your Python code will be graded according to our class Rubric.
9. Submit your Python code file. Name it “XXXX-Romeo.py” where XXXX is your name.
10. Submit also a Word document showing screen shots of the various testing conditions. Good
programmers test all cases, so you should make sure you show tests for erroneous inputs.
Be sure to comment your program adequately!
Assignment #2
Write a program that categorizes each mail message by which day of the week the commit was done. To
do this look for lines that start with “From”, then look for the third word and keep a running count of
each of the days of the week. At the end of the program print out the contents of your dictionary (order
does not matter).
The input file you will use for this is the ‘mbox.txt’ file, first introduced in Severance Section 7.3. It can
be found at http://www.pythonlearn.com/code3/mbox.txt. A shorter file for debugging can be found at
http://www.pythonlearn.com/code3/mbox-short.txt.
1. Remember your Python code will be graded according to our class Rubric.
2. Submit your Python code file. Name it “XXXX-day.py” where XXXX is your name.
3. Submit also a Word document showing screen shots of the various testing conditions. Good
programmers test all cases, so you should make sure you show tests for erroneous inputs. Make
sure you submit test results from the full mbox.txt file (and not from the mbox-short.txt file.)
Be sure to comment your program adequately!
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
chevron_up_icon
1 out of 1
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]