In: Computer Science
Use python.
redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
The explanation is done in the code itself. If you have any queries write a comment. If you have understood or if it is helping then give an upvote., Thank you. Redacted means to remove or hide here i have removed them .
SOLUTION:
import re
def redact_file(name):
##open the given file in read mode
file=open(name,"r")
##split the input file by using the period
outfile=name.split(".")
##create a new file
outfileName=outfile[0]+"_redacted."+outfile[1]
##open the output file
writeFile=open(outfileName,"w")
for data in file.readlines():
##divide a string into list by using split
lineData=data.split(" ")
##pattern to redact numbers
pattern = '[0-9]'
##by using the pattern eleminate numbers froom i t
lineData = [re.sub(pattern, '', i) for i in lineData]
##join the list to form a sting and write it to the output
file
writeFile.write(" ".join(lineData))
writeFile.close()
file.close()
##call the function I have kept the file in the same directory so I
have a name if you don't have code and file in same directly ##give
the complete path
redact_file("testingDoc.txt")
CODE image:
INPUT FILE:
OUTPUT FILE: