在 python 2 中将 int 时间戳转换为 datetime 时遇到问题

Having trouble converting int timestamp to datetime in python 2

我是 Python 世界的新手,正在做一个项目。该项目是从用户数据的 CSV 中读取并处理数据以创建关于用户健康的不同报告。

其中一个报告是计算上周有多少用户登录。 'last login' 数据是字符串,但我转换为 int。我将 'last login' 附加到一个单独的列表中。

一些用户没有登录,所以没有 'last login.' 我用 0 替换了空索引,所以没有索引是空的。我正在努力将时间戳转换为日期时间(实际上是任何格式:MM/DD/YYYY、HH:MM:SS)。我不断收到错误消息:

ValueError: year is out of range

我试过除以 1000,但仍然有问题。有什么想法吗?

下面是我在列表中使用的数据示例。

[1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]

用于获取错误的代码:

intLastLogin = [int(x) for x in lastLogin]

for row in intLastLogin:
    dt = datetime.fromtimestamp(row).strftime('%Y-%m-%d %H:%M:%S')
    print dt

您可以使用 datetime.datetime.fromtimestamp() 将毫秒转换为日期时间。例如:

from datetime import datetime

timestamps = [1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]

datetimes = [datetime.fromtimestamp(t/1000) for t in timestamps]

print datetimes[0]
# OUTPUT
# 2018-12-12 15:00:10

您可能还想添加一个条件,即 return 是您插入 0 作为值的项目的日期时间对象以外的对象(否则,这些项目将 return 1970-01-01 00:00:00.

的日期时间
from datetime import datetime
data=[1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]
data=[datetime.utcfromtimestamp(int(x)/1000).strftime("%Y-%m-%d %H:%M:%S") for x in data]
print(data)