简体   繁体   English

python 2.7如何读取日期

[英]python 2.7 how to read in dates

I have a python 2.7 question 我有一个python 2.7问题

My data has the format below. 我的数据具有以下格式。 (it is stock market data) The data name is finallist and it is a list in python. (它是股市数据)数据名称是finallist,它是python中的列表。

[['AGGP', '15-Mar-18', '19.22', '19.25', '19.2', '19.23', '26800\n'],
 ['AGGY', '15-Mar-18', '49.17', '49.18', '49.09', '49.16', '42500\n'],
 ['AGQ', '15-Mar-18', '31.6', '31.605', '31.27', '31.3', '112900\n'],
 ['AGT', '15-Mar-18', '31.83', '31.83', '31.81', '31.81', '2600\n'],
 ['AADR', '6-Mar-18', '60.4', '60.58', '60.17', '60.4', '18200\n'],
 ['AAMC', '6-Mar-18', '65.2', '65.2', '65.2', '65.2', '100\n'],    
 ['AAU', '6-Mar-18', '0.89', '0.9', '0.86', '0.87', '147500\n'],
 ['ABE', '6-Mar-18', '15.17', '15.29', '15.12', '15.2', '13700\n']]

I am trying to sort by the 2nd column of the list but making sure the program understands the values is a date. 我正在尝试按列表的第二列进行排序,但要确保程序理解这些值是一个日期。

I have tried the following and it does not work. 我已经尝试了以下方法,但它不起作用。

import datetime import time finallist.sort(key=lambda finallist: datetime.strptime(finallist[0][0][1] , '%d-%b-%y'))

It Should sort the list by the dates reading each date as a date but it does not. 它应该按将日期读为日期的日期对列表进行排序,但事实并非如此。 Could anyone offer any thoughts on how to do this? 谁能对如何做到这一点提出任何想法? Apologies if this is repeated but I looked at several other examples on line but none seemed to work or match my situation. 抱歉,如果重复一遍,但我在线查看了其他几个示例,但似乎都没有奏效或符合我的情况。

Thanks in advance. 提前致谢。

You'll basically just want 你基本上只是想要

import datetime
finallist.sort(key=lambda row: datetime.datetime.strptime(row[1], '%d-%b-%y'))

but another approach, which is useful if you need to further process the data, is to preprocess the datetimes first: 但是另一种方法(如果需要进一步处理数据很有用)是先对日期时间进行预处理:

import datetime

data = [
    ["AGGP", "15-Mar-18", "19.22", "19.25", "19.2", "19.23", "26800\n"],
    ["AGGY", "15-Mar-18", "49.17", "49.18", "49.09", "49.16", "42500\n"],
    ["AGQ", "15-Mar-18", "31.6", "31.605", "31.27", "31.3", "112900\n"],
    ["AGT", "15-Mar-18", "31.83", "31.83", "31.81", "31.81", "2600\n"],
    ["AADR", "6-Mar-18", "60.4", "60.58", "60.17", "60.4", "18200\n"],
    ["AAMC", "6-Mar-18", "65.2", "65.2", "65.2", "65.2", "100\n"],
    ["AAU", "6-Mar-18", "0.89", "0.9", "0.86", "0.87", "147500\n"],
    ["ABE", "6-Mar-18", "15.17", "15.29", "15.12", "15.2", "13700\n"],
]

for datum in data:
    datum[1] = datetime.datetime.strptime(datum[1], "%d-%b-%y")

data.sort(key=lambda datum: datum[1])

It should look like this: 它看起来应该像这样:

from datetime import datetime

sorted(finallist, key=lambda x: datetime.strptime(x[1], '%d-%b-%y'))

#[['AADR', '6-Mar-18', '60.4', '60.58', '60.17', '60.4', '18200\n'],
# ['AAMC', '6-Mar-18', '65.2', '65.2', '65.2', '65.2', '100\n'],
# ['AAU', '6-Mar-18', '0.89', '0.9', '0.86', '0.87', '147500\n'],
# ['ABE', '6-Mar-18', '15.17', '15.29', '15.12', '15.2', '13700\n'],
# ['AGGP', '15-Mar-18', '19.22', '19.25', '19.2', '19.23', '26800\n'],
# ['AGGY', '15-Mar-18', '49.17', '49.18', '49.09', '49.16', '42500\n'],
# ['AGQ', '15-Mar-18', '31.6', '31.605', '31.27', '31.3', '112900\n'],
# ['AGT', '15-Mar-18', '31.83', '31.83', '31.81', '31.81', '2600\n']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM