Question

You must write a function that works like a small editor. It should open an existing...

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

Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code which will read in strings from nemo.txt, then call Lab08(string) on each line of nemo.txt. Your job is to write Lab08.m. The Lab08 function will take in a string as input and will return an integer n as output. The value of n will be the nth word in the input string, where n is the location of the substring 'Nemo'. Please note that...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
Point/Counterpoint from chapter 16. Take a stand. Do you agree or disagree? Write a minimum of...
Point/Counterpoint from chapter 16. Take a stand. Do you agree or disagree? Write a minimum of one paragraph. Chapter 16 The Hierarchical Structure: The Superior Format Point Yes The hierarchy is the enduring foundation for how MNEs optimally arrange the roles, responsibilities, and relationships of its structure for a simple reason—it is the superior format for doing so. It sets a clear chain of command, functional span of control, effective allocation of authority, and precise assignment of tasks. It specifies...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich,...
Mattel Responds to Ethical Challenges Business Ethics This case was written by Debbie Thorne, John Fraedrich, O. C. Ferrell, and Jennifer Jackson, with the editorial assistance of Jennifer Sawayda. This case was developed for classroom discussion rather than to illustrate either effective or ineffective handling of an administrative, ethical, or legal discussion by management. All sources used for this case were obtained through publicly available material. Mattel, Inc. is a world leader in the design, manufacture, and marketing of family...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT