Need to convert a date and price from a list into appropriate date format & currency - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Need to convert a date and price from a list into appropriate date format & currency (/thread-16995.html) |
Need to convert a date and price from a list into appropriate date format & currency - SilverLeaf90 - Mar-23-2019 Can anyone lend me some help on this issue. I need to print from a list that contains some data. But 2 of the elements in that list are different than plain strings. 1 is a date and the other is a price. Example code: This is a constant named PURCHASERS that contains the data. I need to print this out and convert the 3rd & 4th elements to English date format and American currency. PURCHASERS = [["GH456", "Alexander", "Apples", date(2005,3,6), 3.89 ] ["GH289", "Billy", "Pears", date(2005,7,11), 2.99], ["GH089", "Todd", "Cereal", date(2005,8,10), 4.15], ["GH223", "Fred", "Bread", date(2005,1,5), 2.89]] This is what I have written so far, but it isn't working at all. I figured I could call the 3rd element to be turned into a string, and then turned into the American format. from datetime import date import locale as lc PURCHASERS = [["GH456", "Alexander", "Apples", date(2005,3,6), 3.89 ] ["GH289", "Billy", "Pears", date(2005,7,11), 2.99], ["GH089", "Todd", "Cereal", date(2005,8,10), 4.15], ["GH223", "Fred", "Bread", date(2005,1,5), 2.89]] def main(): print("NUMBER\t\tNAME\tITEM-TYPE\t\tDATE\t\tPRICE ") lc.setlocale(lc.LC_ALL, 'us') date_purchased = date.PURCHASERS(3) print(PURCHASERS.date_purchased.strftime("%d %b %Y")) if __name__ == "__main__": main()I need my output to look like a nice table too :
RE: Need to convert a date and price from a list into appropriate date format & currency - Yoriz - Mar-23-2019 in the line PURCHASERS = [["GH456", "Alexander", "Apples", date(2005,3,6), 3.89 ]a , is missing off the end the following line is all kinds of wrong date_purchased = date.PURCHASERS(3)you index items in a list with [] not () and PURCHASERS is not a method or attribute of date in the line print(PURCHASERS.date_purchased.strftime("%d %b %Y"))date_purchased is not a method or attribute of the list PURCHASERS it should just be date_purchased.strftime("%d %b %Y")fix these things first. |