Python Forum
Need to convert a date and price from a list into appropriate date format & currency
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to convert a date and price from a list into appropriate date format & currency
#1
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 Huh :

Output:
NUMBER NAME ITEM TYPE DATE PRICE GH456 Alexander Apples 3/6/2005 $3.89
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert every even number in a list to odd? Bruizeh 4 3,750 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  date and time jordex02 2 2,070 Dec-05-2019, 03:39 PM
Last Post: ichabod801
  Convert a list of integers to strings? Cornelis 3 2,260 Nov-15-2019, 12:13 PM
Last Post: perfringo
  how to convert list into string Shevach 3 2,623 May-14-2019, 09:51 AM
Last Post: perfringo
  How to plot date series in matplotlib? StrybolData 2 8,360 Jan-25-2018, 07:13 PM
Last Post: StrybolData
  Displaying number of currency danellapotter 3 2,762 Jan-16-2018, 07:58 PM
Last Post: buran
  creating date/time stamp from dataframe values kiki1113 1 2,506 Dec-06-2017, 05:43 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020