Question

In: Computer Science

use Python datetime module, strftime Write a program that processes the following textfile and prints the...

use Python datetime module, strftime

Write a program that processes the following textfile and prints the report shown below.

Textfile:

  • Ask the user for the filename and BE SURE TO CHECK THAT IT EXISTS.
  • The file will contain lines which each contain 2 strings (bookTitle and publicationDate) separated by a comma and a space. Note that the title of the book might contain spaces!
  • Example of the textfile:

Gone Girl, 5-24-2012
To Kill A Mockingbird, 7-11-1960
Harry Potter and the Sorcerer’s Stone, 6-26-1997

Output: print the title and the publication date formatted in two ways (using strftime)

Example output:

Title                                                     Publication Date           Alternate Date

Gone Girl                                             May 24, 2012                5/24/12
To Kill A Mockingbird                            July 11, 1960                 7/11/60
Harry Potter and the Sorcerer’s Stone    June 26, 1997               6/26/97

Solutions

Expert Solution

Please find below modified code and don't forget to give a Like.

Please refer screenshot for indentation and output.

Modified code as per user requirements:

from os.path import*
from datetime import*
def main():
    title=[]
    pub_date=[]
    alternate_date=[]
    name=input("Enter file name")
    title,pub_date,alternate_date=readfile(name)
    printReport(title,pub_date,alternate_date)
def readfile(name):
    file = open(name, "r")
    list1 = file.readlines()
    title = []  # this is for title names
    date = []  # this is for date unformatted list
    for i in list1:
        list2 = i.split(",")
        title.append(list2[0])
        if (list2[1].startswith(" ")):  # this is for validation startswith spaces& endswith \n
            if (list2[1].endswith("\n")):
                date.append(list2[1][1:(len(list2[1]) - 1)])
            else:
                date.append(list2[1][1:len(list2[1])])
        else:
            if (list2[1].endswith("\n")):
                date.append(list2[1][0:(len(list2[1]) - 1)])
            else:
                date.append(list2[1][0:len(list2[1])])
    pub_date = []  # for formated publication date
    alternat_date = []  # for formated alternate date
    for i in date:
        date_list = i.split("-")
        current_timestamp = datetime(int(date_list[2]), int(date_list[0]), int(date_list[1]))
        pub_date.append(current_timestamp.strftime("%b %d,%Y"))
        alternat_date.append(current_timestamp.strftime("%m/%d/%Y"))
    file.close()
    return title,pub_date,alternat_date
def printReport(title,pub_date,alternat_date):
    print("Title                                Publication Date        Alternate Date")
    print(title[0],"                              ",pub_date[0],"         ",alternat_date[0])
    print(title[1], "\t\t\t        ", pub_date[1], "         ", alternat_date[1])
    print(title[2], "", pub_date[2], "         ", alternat_date[2])

    #for i in range(3):
    #    print(title[i],"\t\t",pub_date[i],"\t\t",alternat_date[i])
main()

Screenshot and output:


Related Solutions

Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: 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...
In Python... Write a program that randomly assign participants for a study (use the random module)....
In Python... Write a program that randomly assign participants for a study (use the random module). The program should: Display a message stating its goal Prompt the user to enter Number of participants in the study (sample size) Minimum age of the participants Maximum age of the participants Based on these specifications, randomly assign for each participant: Gender (you can use "1" for Male and "2" for Female) Age The program should calculate and display: Total number of male participants...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
Write a Python program that reads in an amount in cents and prints the dollar amount...
Write a Python program that reads in an amount in cents and prints the dollar amount in that and the remaining value in cents. For example, if the amount reads in is 360 (cents), the program would print 3 dollars and 60 cents. if the amount read in is 75 (cents), the program would print 0 dollars and 75 cents.
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
Python # Write a program that examines three variables—x, y, and z # and prints the...
Python # Write a program that examines three variables—x, y, and z # and prints the largest odd number among them. # If none of them are odd, it should print a message to that effect. n = input('Enter the 1st Integer x: ') x = int(n) n = input('Enter the 2nd Integer y: ') y = int(n) n = input('Enter the 3rd Integer z: ') z = int(n) if x % 2 == 0 and y % 2 ==...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT