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...
Specification This script reads in class data from a file and creates a dictionary. It then...
Specification This script reads in class data from a file and creates a dictionary. It then prints the data from the dictionary. The script has the following functions class_tuple_create print_classes class_tuple_create This function has the following header def class_tuple_create(filename): The function reads in a file with four fields of data about a class. The fields are Course ID Room number Instructor Start time The function must print an error message if the file cannot be opened for any reason. The...
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.
1. Create a Python module that reads Linux_syslog_ip_parser.py that reads syslog file and parses the IPV4...
1. Create a Python module that reads Linux_syslog_ip_parser.py that reads syslog file and parses the IPV4 addresses and stores the result into a csv file. Your module should contain the following functions: a. linux_logfile_reader(filename) function that reads the syslog file and returns all IPV4 addresses b. count_ipfrequency(ip_list) function that accepts the list of IPV4 addresses retuned by linux_logfile_reader function and returns a dictionary list with ip:frequency pair. c. Writeto_csv_file(ip_frequency) function that accepts a dictionary list containing ip:frequency pair returned from...
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. //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT