Question

In: Computer Science

Write a program in python that counts the number of words in President Abraham Lincoln’s Gettysburg...

Write a program in python that counts the number of words in President Abraham Lincoln’s Gettysburg Address from the file provided. Replace all instances of "nation" with country, and all instances of "we" with "Americans". Write the revised address to a new file

its for a txt file

Solutions

Expert Solution

f=open(input("Enter filename:"),'r')#open file in read mode
l=list(f.readlines())
for i in range(len(l)):
l[i]=l[i][:-1]#remove new line characters

c=0
for i in l:
temp=i.split(" ")
c=c+len(temp)#get the number of words in each line and add to c
temp=[]
print("The count is: ",c)#print total number of words

res=[]
for i in range(len(l)):
temp=l[i].split(" ")
for j in range(len(temp)):
if temp[j]=="nation":#check if word is nation and replace it
temp[j]="country"
if temp[j]=="we":#check if word is we and replace it
temp[j]="Americans"
res.append(' '.join(temp))#append the lines to list res

file=open("resultaddress.txt",'w')#open file to write
for i in res:
file.write(i+"\n")#write the data to file and close it
file.close()

Screenshots:

The screenshots are attached below for reference.

Please follow them for output and proper indentation.

Please upvote my answer. Thank you.

The input file provided is as follows.

The output resultant file that is generated by the program is as follows.



Related Solutions

write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a...
Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a C Language Program that will search for the following words and then print out the entire sentence from Lincoln’s Gettysburg Address for every occurrence of each of the specified words:             civil                  nation              nobly               people             place               proper             Your program should search through the Gettysburg Address and find the answers, not pre-load them to print out once a particular word is sought. This is...
The Gettysburg Address President Abraham Lincoln November 19, 1863 "Fourscore and seven years ago our fathers...
The Gettysburg Address President Abraham Lincoln November 19, 1863 "Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of...
How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
Write a python program that calculates the average number of words per sentence in the input...
Write a python program that calculates the average number of words per sentence in the input text file. every sentence ends with a period (when, in reality, sentences can end with !, ", ?, etc.) and the average number of sentences per paragraph, where a paragraph is any number of sentences followed by a blank line or by the end of the text.
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a program in python that corrects misspelled words. The input for the program is either...
Write a program in python that corrects misspelled words. The input for the program is either a string or a list of strings. The output should be a string or a list depending on the input and should have the spellings corrected. The speller should be able to correct at least the words listed below. You are free to use any data structures we have covered so far including lists and dictionaries. Your program should be in autocorrect.py. Here is...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Write a C program that counts the number of non white-space characters in an inputtext file....
Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file. The command format is as follows: command -f inputfile outputfile or, command -s inputfile -f indicates writing to an output file; -s indicates displaying the output on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT