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

标签 python python-2.7 datetime timestamp python-datetime

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

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

有些用户还没有登录,因此没有“上次登录”。我用 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

您可能还想添加一个条件,为您插入 0 作为值的项目返回 datetime 对象以外的其他内容(否则,这些项目将返回 的日期时间1970-01-01 00:00:00

关于python - 在 python 2 中将 int 时间戳转换为 datetime 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887943/

相关文章:

python - 为什么 Django 的 `urlencode` 不编码斜杠?

python - Python 中的惰性排序

python - 将 Pandas 列转换为 DateTime

ruby 日期时间 : why is 60 seconds being treated as 59 seconds?

php - 这是一个 php 错误,还是我的时区/PHP 设置有问题?

python - 以文本文件和 oracle 表为源的 Hive、Pig 或 Python Mapreduce 哪个能提供最佳性能?

python - 使用 pandas 数据框和组合条件会产生歧义错误

python-2.7 - 在 Python 2.7 中是否有显示可复制文本的消息框?

python - 为什么 python 的列表切片不会产生索引越界错误?

python - 函数的返回值可以在where子句中传递吗