Question

In: Computer Science

Implement in Python a script that does the following: 1) reads input from a supposed file...

Implement in Python a script that does the following:

1) reads input from a supposed file called firstnames_2.txt.

2) processes the input and writes and saves the output to a file.

NOTE:

Please make sure that

  • the names are written in the outfile with one name on each line
  • no comma ( , ) after the name in the output

Solutions

Expert Solution

# this function read and write file
def read_and_write(ifilename, ofilename):
    name_list=[]
    # open file to read
    fpi=open(ifilename,"r")

    # read each lines
    for line in fpi:
        # content of line
        name=line
        # write the names along with the name length
        name_list.append(name)
    # close files
    fpi.close()

    # open file to write
    fpo = open(ofilename, "w")
    for i in range(len(name_list)):
        fpo.write(name_list[i]);
        fpo.write(str(len(name_list[i])-1));
        fpo.write("\n");    
    fpo.close()
    print("Output file has been written.")
        
        
        
# main function
def main():
    input_filename = "firstnames_2.txt"
    output_filename = "output.txt"
    # call function to read  and write input
    read_and_write(input_filename, output_filename)
    
    
# driver code
if __name__ == "__main__":
    main()

___________________________________________________________________

___________________________________________________________________

Alex
Tom
Hans
Roger
Harris
Robert

___________________________________________________________________

Alex
4
Tom
3
Hans
4
Roger
5
Harris
6
Robert
6

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
For this problem, you'll be writing a script that reads the content of a file, modifies...
For this problem, you'll be writing a script that reads the content of a file, modifies that content, and then writes the result to a new file. a. Define a function called modified() that takes a string and returns a "modified" version of the string. The type of modification is up to you (be creative!), but I have a couple requirements: You must make at least two different modifications, and at least one of those modifications must be some kind...
Implement a function that reads numbers in from a file with one number per line and...
Implement a function that reads numbers in from a file with one number per line and outputs all the possible sums that can be formed by subsets of the numbers. For instance, if the numbers in the file are 1 2 4, then the output would be 0, 1, 2, 4, 3, 5, 6, 7. Note that 0 is in the output because it uses none of the numbers, while 7 is the sum of all of the numbers. //...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT