[英]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.
我需要输入一个日期(例如3/21/2012)并将其转换为以下格式:2012年3月21日。
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)
You should really specify what about your code is not working (ie, 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'
. 由于Python不会尝试切分
'%B'
来做您想'%B'
。 You should instead use '%b'
, which as noted in the documentation for strftime() , corresponds to the locale-abbreviated month name. 您应该改用
'%b'
,它在strftime()的文档中已指出 ,它对应于语言环境缩写的月份名称。
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. 对于它的价值,我正在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. 我还
from __future__ import print_function
获得了这一行from __future__ import print_function
以使其与您似乎正在使用的Python3打印功能兼容。
Check out
Dateutil .
签出
Dateutil 。
You can use it to parse a string into python datetime object and then print that object using strftime.
您可以使用它将字符串解析为python datetime对象,然后使用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. 最好使用
strptime
并指定所需的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.