user1251230
user1251230

Reputation: 11

Python date string mm/dd/yyyy to datetime

I have to write a program where I take stocks from yahoo finance and print out certain information for the site. One of the pieces of data is the date. I need to take a date such as 3/21/2012 and converter to the following format: Mar 21, 2012.

Here is my code for the entire project.

def getStockData(company="GOOG"):

    baseurl ="http://quote.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"

    url = baseurl.format(company)
    conn = u.urlopen(url)
    content = conn.readlines()
    data = content[0].decode("utf-8")
    data = data.split(",")
    date = data[2][1:-1]
    date_new = datetime.strptime(date, "%m/%d/%Y").strftime("%B[0:3] %d, %Y")
    print("The last trade for",company, "was", data[1],"and the change was", data[4],"on", date_new)


company = input("What company would you like to look up?")
getStockData(company)


co = ["VOD.L", "AAPL", "YHOO", "S", "T"]
for company in co:
    getStockData(company)

Upvotes: 1

Views: 3763

Answers (2)

Kien Truong
Kien Truong

Reputation: 11381

Check out Dateutil. You can use it to parse a string into python datetime object and then print that object using strftime.

I've since come to a conclusion that auto detection of datetime value is not always a good idea. It's much better to use strptime and specify what format you want.

Upvotes: -1

ig0774
ig0774

Reputation: 41257

You should really specify what about your code is not working (i.e., what output are you getting that you don't expect? What error message are you getting, if any?). However, I suspect your problem is with this part:

strftime('%B[0:3] %d, %Y')

Since Python won't do what you think with that attempt to slice '%B'. You should instead use '%b', which as noted in the documentation for strftime(), corresponds to the locale-abbreviated month name.


EDIT

Here is a fully functional script based on what you posted above with my suggested modifications:

import urllib2 as u
from datetime import datetime

def getStockData(company="GOOG"):
    baseurl ="http://quote.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"

    url = baseurl.format(company)
    conn = u.urlopen(url)
    content = conn.readlines()
    data = content[0].decode("utf-8")
    data = data.split(",")
    date = data[2][1:-1]
    date_new = datetime.strptime(date, "%m/%d/%Y").strftime("%b %d, %Y")
    print("The last trade for",company, "was", data[1],"and the change was", data[4],"on", date_new)

for company in ["VOD.L", "AAPL", "YHOO", "S", "T"]:
    getStockData(company)

The output of this script is:

The last trade for VOD.L was 170.00 and the change was -1.05 on Mar 06, 2012
The last trade for AAPL was 530.26 and the change was -2.90 on Mar 06, 2012
The last trade for YHOO was 14.415 and the change was -0.205 on Mar 06, 2012
The last trade for S was 2.39 and the change was -0.04 on Mar 06, 2012
The last trade for T was 30.725 and the change was -0.265 on Mar 06, 2012

For what it's worth, I'm running this on Python 2.7.1. I also had the line from __future__ import print_function to make this compatible with the Python3 print function you appear to be using.

Upvotes: 2

Related Questions