using python 3, I ALWAYS RATE MY ANSWERS. tHANKS Copy princess.txt from my 153 p
ID: 3875562 • Letter: U
Question
using python 3, I ALWAYS RATE MY ANSWERS. tHANKS
Copy princess.txt from my 153 public folder on smaug to your folder that contains myfunctions.py
1.Construct a list of all punctuation characters (use the string module classification set)
2.Use a list method to remove the apostrophe character (') from the above list
3.Prompt the user to enter the name of a file to read
4.For each line in the file
5.Use string methods to make it all lower case and remove any leading or trailing white space
6.Call your replace_chars function with the modified line (from step 5) and the list from step 2
7.Display the modified line (from step 6) preceded by the line number. Use the string format method in this step.
Here is princess.txt
Grandson|Cough, cough, cough. Cough, cough, cough. {Grandson is on the bed, playing video game.}
Mother|{Enters.} Hi, honey.
Grandson|Hi, Mom.
Mother|{Kisses son and feels his forehead.} You feeling any better?
Grandson|A little bit.
Mother|Guess what?
Grandson|What?
Mother|Your Grandfather's here. {Opens curtains.}
Grandson|Mom, can't you tell him I'm sick?
Mother|You're sick? That's why he's here.
Grandson|He'll pinch my cheek. I hate that.
Mother|Maybe he won't.
Grandfather|{Entering with a flourish.} Heyyyy!! How's the sickie? Heh? {Pinches boy's cheek. Boy looks at mother accusingly.}
Mother|I think I'll leave you two pals alone. {Exits.}
Grandfather|I brought you a special present.
Grandson|What is it?
Grandfather|Open it up.
Grandson|{Opens the package. Disappointed.} A book?
Grandfather|That's right. When I was your age, television was called books. And this is a special book. It was the book my father used to read to me when I was sick, {takes book} and I used to read it to your father. And today I'm gonna read it to you.
Grandson|Has it got any sports in it?
Grandfather|Are you kidding? Fencing, fighting, torture, revenge, giants, monsters, chases, escapes, true love, miracles...
Grandson|Doesn't sound too bad. I'll try to stay awake. {Turns off TV.}
Grandfather|Oh, well, thank you very much, very nice of you. Your vote of confidence is overwhelming. All right. {Puts glasses on.} The Princess Bride, by S. Morgenstern. Chapter One. Buttercup was raised on a small farm in the country of Florin
HERE IS REPLACE_CHARS
def replace_chars(s,li):
for c in li:
s= s.replace(c," ")
return s
if __name__=="__main__":
import test
print('testing function')
test.testEqual(replace_chars(" SampIEam*", ["p", "E", "*"])," Sam I am ")
test.testEqual(replace_chars(" Test1", ["e", "1"])," T st ")
test.testEqual(replace_chars(" Salt@12", ["S", "@"])," alt 12")
Explanation / Answer
Hi,
the code below should work for the same.
f = open('princess.txt"', 'r')
s = f.read() #read file
exclude = set(string.punctuation)
l = " ".join(ch for ch in s if ch in exclude) #join all punctuations with space
punct = list(set(l.split(" "))) #split all punctuations into a list
punct.remove("'") #remove apostrophe
filename = raw_input("Enter filename ") #ask user for filename
f = open(filename, 'r') #open the file
text = f.read()
lines = [text.lower() for line in filename] #convert file text to lower case
lines = ''.join(lines.split()) #remove all whitespaces
newline= replace_chars(lines,punct) #call the function given
for line in newline:
line.strip() #print each line with line numbers
print(line)
open_file.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.