You must write a function that works like a small editor. It should open an existing file (let us say this is File1.txt. This file must exist before the function is run), read it line by line. Then write it to another file (let us say this is File2.txt. This file need not exist since Python will create it). However, if a certain line exists in the first file, it should be replaced by another line supplied by you. You must submit the source file and code. I do not need the destination file since that will be created by your code.
Hint of rough outline of code:
You must create a text file with Notepad and save it as File1.txt or you can call it name_source, e.g. Sanford_source.txt. Write whatever you want into this file. You can copy and paste anything you find in the internet provided it is simple text. Throughout this file add a signature line that will be replaced. E.g.
dan_source.txt may have to following content:
'Once upon a time there lived a lovely princess with fair skin and blue eyes.
She was so fair that she was named Snow White.
Dan line that must be edited out.
Her mother died when Snow White was a baby and her father married again.
This queen was very pretty but she was also very cruel.
Dan line that must be edited out.
The wicked stepmother wanted to be the most beautiful lady in the kingdom.
She would often ask her magic mirror, “Mirror! Mirror on the wall! Who is the fairest of them all?”
The magic mirror would say, “You are, Your Majesty!”
Dan line that must be edited out.
But one day, the mirror replied, “Snow White is the fairest of them all!”
The wicked queen was very angry and jealous of Snow White.
She ordered her huntsman to take Snow White to the forest and kill her.
Dan line that must be edited out.
“I want you to bring back her heart,” she ordered.
The huntsman took pity on Snow White and set her free.
Dan line that must be edited out.
He took a deer's heart to the wicked queen and told her that he had killed Snow White.
Dan line that must be edited out.
Snow White wandered in the forest all night, crying.'
In several places in the above file I added “Dan line that must be edited out’ You should add a similar sentence with your own name in seven or more places in your source file. This line must be replaced by ‘Dan replaced the original line here’ again with your name instead of Dan. Call the file that you write into with the replacement Dan_dest.txt (again with your name).
Your source and destination files must be in different directories on your computers and not in the current working directory of Python. That means when you are reading in the source and writing the destination file, you must provide full paths
E.g. I may write code as
def dan_edit(str1, str2):
infil= open('C:/DataAnalysis/sample1/chel_source.txt','r')
outfil=open('C:/mydocs/samples/chel_dest.txt','w')
where srt1 is the line that must be edited out (i.e. Dan line that must be edited out) and str2 is the line that will be put in its place (i.e. Dan replaced the line here).
You should use the line function to read the source file line by line e.g.
for line in infil: --> this will read each line in the source and check if the line that was read is similar to the one that should be substituted, you must use the replace attribute of the line function
repline = line.replace(str1,str2) ---> this will replace str1 with str2, if the line that was read happens to be similar to str1. This code will execute only if the line that was read is similar to str1. If it is, then it will replace with str2 and assign it to repline. If the line is not similar to str1, then the line is left as it is.
Next this must be written into the destination file
outfil.write(repline)---> this will write the line into the destination file. Remember all of this is a part of the for loop.
You will call this function i.e. use it at the Spyder prompt with the following function call: dan_edit(‘Dan line that must be edited out.’, ‘Dan replaced the line here’)
Note that I used forward slash in the file paths. Python uses forward slashes not backslash that windows uses.
NB: Please follow hint format and provide a picture of spider code
Get Answers For Free
Most questions answered within 1 hours.