将一个int数组转换为Date-Objects

时间:2016-06-29 14:32:45

标签: python arrays

我有一组我要转换为日期类型对象的整数:

[[19480916 19480901 19480917 19480901 19480901 19481019]
 [19480917 19480916 19481019 19480922 19480922 19490902]
 [19481004 19480917 19481021 19480924 19481004 19501124]]

我有以下代码应该遍历数组并将每个数字转换为日期对象并将其附加到新列表:

Date_List = []
from datetime import date, datetime, timedelta
for s in myArray:
    date1 = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
    Date_List.append(date1)

当我运行它时,我收到以下错误:

TypeError: only length-1 arrays can be converted to Python scalars.

有没有办法让我能够做到这一点?

2 个答案:

答案 0 :(得分:4)

使用str()将每个数字转换为字符串,并使用strptime()将字符串加载到datetime对象中:

from datetime import datetime

l = [
    [19480916, 19480901, 19480917, 19480901, 19480901, 19481019], 
    [19480917, 19480916, 19481019, 19480922, 19480922, 19490902], 
    [19481004, 19480917, 19481021, 19480924, 19481004, 19501124]
]

for sublist in l:
    print([datetime.strptime(str(item), "%Y%m%d") for item in sublist])

打印:

[datetime.datetime(1948, 9, 16, 0, 0), datetime.datetime(1948, 9, 1, 0, 0), datetime.datetime(1948, 9, 17, 0, 0), datetime.datetime(1948, 9, 1, 0, 0), datetime.datetime(1948, 9, 1, 0, 0), datetime.datetime(1948, 10, 19, 0, 0)]
[datetime.datetime(1948, 9, 17, 0, 0), datetime.datetime(1948, 9, 16, 0, 0), datetime.datetime(1948, 10, 19, 0, 0), datetime.datetime(1948, 9, 22, 0, 0), datetime.datetime(1948, 9, 22, 0, 0), datetime.datetime(1949, 9, 2, 0, 0)]
[datetime.datetime(1948, 10, 4, 0, 0), datetime.datetime(1948, 9, 17, 0, 0), datetime.datetime(1948, 10, 21, 0, 0), datetime.datetime(1948, 9, 24, 0, 0), datetime.datetime(1948, 10, 4, 0, 0), datetime.datetime(1950, 11, 24, 0, 0)]

答案 1 :(得分:2)

它不起作用的原因是因为首先你试图将列表的一部分作为参数传递给datetime()

在for循环for s in myArray中,s的每次迭代看起来像这样:[19480916, 19480901, 19480917, 19480901, 19480901, 19481019]

因此,当您尝试传递s[0:4]时,实际上是将[19480916, 19480901, 19480917, 19480901]放入datetime(year: int)参数,依此类推。

如果您开始迭代先前尝试传入的数组中的每个数字,则下一个问题是整数无法切片,因此您必须在切片之前将每个日期字符串设置为str,然后按原样将其投射到int

参见我的例子:

myArray = [[19480916, 19480901, 19480917, 19480901, 19480901, 19481019],
           [19480917, 19480916, 19481019, 19480922, 19480922, 19490902],
           [19481004, 19480917, 19481021, 19480924, 19481004, 19501124]]

Date_List = []
for array in myArray:
    for string in array:
        s = str(string)
        date1 = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
        Date_List.append(date1)

和我得到的输出:

>>> from pprint import pprint
>>> pprint(Date_List)
[datetime.datetime(1948, 9, 16, 0, 0),
 datetime.datetime(1948, 9, 1, 0, 0),
 datetime.datetime(1948, 9, 17, 0, 0),
 datetime.datetime(1948, 9, 1, 0, 0),
 datetime.datetime(1948, 9, 1, 0, 0),
 datetime.datetime(1948, 10, 19, 0, 0),
 datetime.datetime(1948, 9, 17, 0, 0),
 datetime.datetime(1948, 9, 16, 0, 0),
 datetime.datetime(1948, 10, 19, 0, 0),
 datetime.datetime(1948, 9, 22, 0, 0),
 datetime.datetime(1948, 9, 22, 0, 0),
 datetime.datetime(1949, 9, 2, 0, 0),
 datetime.datetime(1948, 10, 4, 0, 0),
 datetime.datetime(1948, 9, 17, 0, 0),
 datetime.datetime(1948, 10, 21, 0, 0),
 datetime.datetime(1948, 9, 24, 0, 0),
 datetime.datetime(1948, 10, 4, 0, 0),
 datetime.datetime(1950, 11, 24, 0, 0)]

也可以用字符串格式打印:

>>> pprint([str(d) for d in Date_List])
['1948-09-16 00:00:00',
 '1948-09-01 00:00:00',
 '1948-09-17 00:00:00',
 '1948-09-01 00:00:00',
 '1948-09-01 00:00:00',
 '1948-10-19 00:00:00',
 '1948-09-17 00:00:00',
 '1948-09-16 00:00:00',
 '1948-10-19 00:00:00',
 '1948-09-22 00:00:00',
 '1948-09-22 00:00:00',
 '1949-09-02 00:00:00',
 '1948-10-04 00:00:00',
 '1948-09-17 00:00:00',
 '1948-10-21 00:00:00',
 '1948-09-24 00:00:00',
 '1948-10-04 00:00:00',
 '1950-11-24 00:00:00']