Please need help for python labs 3.4.2 Lab 8: Jabberwocky In this lab, you will
ID: 641549 • Letter: P
Question
Please need help for python labs 3.4.2
Lab 8: Jabberwocky
In this lab, you will gain some experience working with files. The primary file we will work with is jabber.txt. I'm assuming you recognize this text. I'll leave it to you to garner participation points by posting the source for this text. [Yes, that's a clandestine fodder prompt.] It has been reformatted so that it now appears with one word per line in the file jabber.txt.
Input file
jabber.txt
Right click to save this file to a work folder. You will create the script for this lab in the same folder.
Basic Overview
The basic structure for this lab is to read through the file and filter the text, that is output only the words that meet a specific criterion.
Minus Version
For the minus version, write a script that reads through jabber.txt. The script will create a file named jabber-selected-words.txt. The new jabber-selected-words.txt file shall contain all of the words in jabber.txt that contain the letter 'j', either upper-case or lower-case. If the word in jabber.txt does not contain the letter 'j', it is "ignored".
There are several ways to approach this problem. Probably the simplest is to process through jabber.txt line by line. Use the readline method of the file object. Here is the basic algorithm in pseudo-code:
open jabber.txt for reading
open jabber-selected-words.txt for writing
read a line from jabber.txt
while there are still lines to be read:
check if the line contains a 'j':
if it does, write it to jabber-selected-words.txt
close the files
For this lab, create the script file in the same folder as your downloaded jabber.txt file. That way, you can use the simple filename to open the file for reading. Similarly, the output jabber-selected-words.txt file will be created in the same folder if you use the simple name.
The completed output file, jabber-selected-words.txt, should contain:
Jabberwock,
jaws
Jubjub
Jabberwock,
Jabberwock?
frabjous
joy.
Notice that the punctuation is included in the lines of jabber.txt and is included in the lines of jabber-selected-words.txt. Also, if you run this script twice, you should only have one copy of the words.
Helpful hint: the in operator will be very useful here.
Check Version
Notice how the minus version prints out all of the words with the letter 'j' in them. For the check version, you create a script that will do something similar for all 26 letters of the English language. To make the output file more manageable, we will change the format for the output. Each letter will be printed using two lines: first a "title" line, then a line with the words. For example, for the 'j' words, this would be:
J words:
Jabberwock, jaws Jubjub Jabberwock, Jabberwock? frabjous joy.
The line of words is created by putting a space between each of the words. The other punctuation you see comes from the jabber.txt file.
The full file will be structured like this:
A words:
`Twas and and wabe; All And
Explanation / Answer
import sys
file = open('jabber.txt','r')
l = []
for lines in file:
l.append(lines[:len(lines)-1])
file.close()
def check():
alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
file = open("jabber-selected-words.txt",'w')
for i in range(26):
file.write(alpha[i]+' words: ')
for j in range(len(l)):
line = l[j].upper()
if alpha[i] in line:
file.write(l[j]+' ')
file.write(' ')
file.close()
def Plus(alpha):
file = open("jabber-selected-words.txt",'w')
for i in range(len(alpha)):
file.write(alpha[i]+' words: ')
for j in range(len(l)):
line = l[j].upper()
if alpha[i] in line:
file.write(l[j]+' ')
file.write(' ')
file.close()
def main():
if (len(sys.argv) == 1):
check()
else:
alpha = []
for i in range(1,len(sys.argv)):
alpha.append(sys.argv[i].upper())
Plus(alpha)
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.