我是Python世界的新手,正在从事一个项目。该项目将从CSV用户数据中读取并处理数据以创建有关用户健康的不同报告。
这些报告之一是找出上周内有多少用户登录。 “上次登录”数据是字符串,但我将其转换为int。我将“上次登录”附加到单独的列表中。
某些用户尚未登录,因此没有“上次登录”。我将空索引替换为0,因此没有索引为空。我正在努力将时间戳记转换为日期时间(实际上是任何格式:MM / DD / YYYY,HH:MM:SS)。我不断收到错误消息:
ValueError:年份超出范围
我尝试除以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
答案 0 :(得分:1)
您可以使用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
的日期时间。
答案 1 :(得分:0)
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)