Lab 5 - Files, Slice/Strip/Split
Directions for Labs
- Submit your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
- It is recommended that students attend in-person lab sections for lots of help!
- If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
- There is no need to attend the section you are enrolled in.
- There is no need to attend any lab sections: there is no attendance taken.
- You can complete the lab on your own time or own computer if you like.
- While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.
- Lots of hints are given. Try to do each task without reading the hints to see what you can figure out! Use the hints to help.
Printing a Text File
- Inside your
cmpt120
folder, create a new folder forlab5
and make alab5.py
file.- See lab 1 for more detailed directions.
- Copy the provided
silence.txt
andromeo.txt
text files into yourlab5
folder. (Those file names are hyper-links) - In your code, open the
silence.txt
file and print all of its contents to the screen.- Hint: See the sample code from lecture on how to open a file.
- Hint: Remember to use the provided code which uses
pathlib
to open a file that is in the same folder as your .py code. See lecture notes / examples. - Hint: If the file will not open, double check you have spelled the file name correctly. Note it's
.txt
, not.csv
. - Hint: This file is just plain text, not a .CSV file. So you will not need to skip any header lines.
- Hint: Use a
for
loop to iterate through all lines of the file. - Hint: Remember that the
for
loop creates a new variable each time through the loop to hold the next line of text. - Hint: Inside your loop, print to the screen each line.
- Hint: Lines read from the file contain a line-feed (enter) at the end. Remove this before printing. What removes it?
- Hint: Use
strip()
.
Print Text File Lines with Last Word
- Find last word in file.
- Edit your code above to also find the last word in the file.
- Assume that the last word has no punctuation around it.
- Once you have found the word, print it to the screen.
- Hint: Use the same
for
loop as above; just add some code to it. - Hint: Create a variable before your loop to store the last word.
- Hint: Initially set this variable to be the empty string.
- Hint: Each time you read a new line, get the last word of the line and store it in your variable.
- Hint: Each line read from the file will have just words (along with punctuation); however, the last word will not have any punctuation around it.
- Hint: How can you break up the line of text? What character do you want to break on instead of a comma?
- Hint: You'll need one of
strip()
,split()
, orslice()
. - Hint: You'll know you found the last word once you have processed every line in the file (i.e., your loop ends).
- Start again from the top of the file so we can read through the file a second time.
- Use
myfile.seek(0)
. - Remember seek(0) will reset your file's "current line" pointer so that the next time your program reads data from the file it will read from the top. The argument (0 in this case) is how many characters from the start of the file.
- Use
- Print out all lines in the file which contain the last word in the file.
- For example, if the last word is "oops", then print all lines in the file which contain the word "oops".
- Hint: You already have the last word in the file (from the first pass of reading the file).
- Hint: Use another
for
loop to iterate through the file a second time. - Hint: Since we called
myfile.seek(0)
above, it has moved us back to the start of the file, so thefor
loop will re-read the whole file as needed. - Hint: For each line, check if the last word is in that line. If so, print it.
- Count the number of lines which contain the last word in the file, and print the count.
- Hint: Use the accumulator pattern.
- Hint: Create a counter variable outside your second for loop.
- Hint: Each time you find a matching line, increment the counter.
- Hint: Print the count. Should this be done before, during, or after the for loop?
- Change to using
romeo.txt
.- Predict how long it will take to process all of Romeo and Juliet!
- Run the code and see how fast the computer is.
Lists and Strings
- Copy the following quote/code in at the bottom of your lab:
# Quote by Lydia Obasi (https://socialjusticeresourcecenter.org/quotes/voting-rights-quotes/) # (Punctuation removed from middle) data = "My vote is my voice and the voice of all who struggled " \ "so that I may have my voice."
- Break the quote into a list of words.
- Hint: I removed all punctuation from inside the quote, but you'll need to remove the
.
at the end. - Hint: What functions can you use to break up the string into words?
- Hint: What function can you use to remove the ending period from the sentence?
- Hint: What order do you need to do these statements in to have a list of words?
- Hint: I removed all punctuation from inside the quote, but you'll need to remove the
- Copy the following code into your file and then edit each print statement so it correctly output what it is asking for. See output below for clarification.
- This exercise uses slices.
- For example, you might change
...{words}")
in one to...{words[5:8]}")
- Note that the
:16
in the first part just aligns the descriptive initial text nicely on the screen. You can leave that part alone.print(f"{'All':16} {words}") print(f"{'First':16} {words}") print(f"{'Last':16} {words}") print(f"{'First 3':16} {words}") print(f"{'Last 5':16} {words}") print(f"{'5th - 10th':16} {words}") print(f"{'All but first 10':16} {words}") print(f"{'All but last 10':16} {words}") print(f"{'All even':16} {words}") print(f"{'All odd':16} {words}") print(f"{'1st letter, 3rd word '} {words}") print(f"{'Last letter, 1st word'} {words}") print(f"{'1st letter, last word'} {words}") print(f"{'Count':16} {words}")
- Expected output:
All ['My', 'vote', 'is', 'my', 'voice', 'and', 'the', 'voice', 'of', 'all', 'who', 'struggled', 'so', 'that', 'I', 'may', 'have', 'my', 'voice'] First My Last voice First 3 ['My', 'vote', 'is'] Last 5 ['I', 'may', 'have', 'my', 'voice'] 5th - 10th ['voice', 'and', 'the', 'voice', 'of', 'all'] All but first 10 ['who', 'struggled', 'so', 'that', 'I', 'may', 'have', 'my', 'voice'] All but last 10 ['My', 'vote', 'is', 'my', 'voice', 'and', 'the', 'voice', 'of'] All even ['My', 'is', 'voice', 'the', 'of', 'who', 'so', 'I', 'have', 'voice'] All odd ['vote', 'my', 'and', 'voice', 'all', 'struggled', 'that', 'may', 'my'] 1st letter, 3rd word i Last letter, 1st word y 1st letter, last word v Count 19
Submission
Submit your lab5.py
file to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)
Topics Covered
- Files
- `open()
for
seek(0)
- Slices / Strip / Split
- Lists
myList[1]
,myList[2][2]
len(myList)
- Iterate list by index
- F-Strings