In: Computer Science
use Python datetime module, strftime
Write a program that processes the following textfile and prints the report shown below.
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
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:

