Unnecessary Censorship^1. It\'s easy: Read in a text file. Replace all 4 letter
ID: 3571454 • Letter: U
Question
Unnecessary Censorship^1. It's easy: Read in a text file. Replace all 4 letter words with. Write the result back out to a new file. For example, text. txt contains the text: Hullabaloo, Hullabaloo, Good-bye to university So long to the orange and the white Good luck to dear old Texas Aggie3 They are the boys that 3how the real. Old fight "The eyes of Texas are upon you" That is the song they 3ing so well Invoke the censor function: >> censor('text.txt'); The file text_censored. txt contains the censored text: Hullabaloo, Cancel! Cancel! Hullabaloo, Cancel! Cancel! Good-bye to Texas university So **** to the orange and the white **** **** to **** old Texas Aggies |**** are the **** **** **** the **** old fight "The **** of Texa3 are **** you" **** the **** **** **** 30 **** the output file name is simply the input filename with _censored appended to the end. >> in = 'text.txt'; >> out = [in(1:end-4) '_censored' in(end-3:end)] out = text_censored.txt. Challenge: Repeat, but read in a list of "banned" words from a file and replace each instance of a banned word in the text file with asterisks.Explanation / Answer
import re
text = open("text.txt","r").read();
cencor = open("cencor.txt","r").read();
t = []
d = text.split()
for i in d:
word1 = " ".join(re.findall("[a-zA-Z]+", i))
if(len(word1)==4):
if(i in cencor):
t.append("****")
else:
t.append(i);
f1 = open("text.txt","w")
f1.write(" ".join(t))
f1.close()
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.