简体   繁体   English

Python日期字符串mm / dd / yyyy到datetime

[英]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.

相关问题 Python-使用日期时间将日期字符串从YYYY-MM-DD转换为DD-MMM-YYYY? - Python - convert date string from YYYY-MM-DD to DD-MMM-YYYY using datetime? 将长日期时间转换为日期 (dd/mm/yyyy) - Converting long datetime into date (dd/mm/yyyy) 在python中将字符串日期转换为DateTime,在python中字符串格式为YYYY-MM-DD Hr-Min-Sec - Converting String Date to DateTime in python where String format is YYYY-MM-DD Hr-Min-Sec in python 将日期时间格式转换为 Python 中的“YYYY-MM-DD HH:MM:SS”字符串 - Convert datetime format to "YYYY-MM-DD HH:MM:SS" string in Python Python 日期时间转换格式为 dd/mm/yyyy HH:MM:SS - Python Datetime convert format into dd/mm/yyyy HH:MM:SS Python:将unicode字符串转换为MM / DD / YYYY - Python: Convert unicode string to MM/DD/YYYY SQLITE3 python 根据字符串日期 yyyy/mm/dd hh:mm:ss 删除行 - SQLITE3 python Delete rows based on the string date yyyy/mm/dd hh:mm:ss Python 在不使用日期时间的情况下将 YYYYMMDD 字符串转换为 YYYY-MM-DD 字符串 - Python Convert YYYYMMDD string to YYYY-MM-DD string Without Using Datetime 如何在 Python 中将 mysql 日期时间 yyyy-mm-dd 格式化为 dd-mm-yyyy? - How to format a mysql datetime yyyy-mm-dd to dd-mm-yyyy in Python? 检查python中数据框中的字符串是否为日期时间(mm-dd-yyyy)格式 - check if a string is in datetime (mm-dd-yyyy) format in a data frame in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM