我有一个JSON文件,其中包含字符串形式的unix时间戳。我试图将这些时间戳转换为人类可读的时间,然后在matplotlib.pyplot中显示。
转换时间戳后,我收到错误:
ValueError:未转换的数据仍然存在:.1806107
如何将日期转换为人类可读形式$('.btn-twitter').on( 'click', function( evt ){
evt.preventDefault();
var tweetURL = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent( $('p.sub_text').text() );
window.open(tweetURL, '_blank');
} )
?
YY-MM-DD HH-MM-SS
答案 0 :(得分:0)
from datetime import datetime
dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]
dates = [datetime.utcfromtimestamp(d).strftime('%F %T') for d in dates]
dates
['2017-06-20 00:25:49',
'2017-06-20 00:25:52',
'2017-06-20 00:25:58',
'2017-06-20 00:26:04',
'2017-06-20 00:26:08']
答案 1 :(得分:0)
如果你改变
dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]
到
dates=[dt.datetime.fromtimestamp(date).strftime('%Y%m%d%H%M') for date in dates]
一切都会好的。
E.g:
from __future__ import print_function
import datetime as dt
dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]
# Error used to occur here
#dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]
dates=[dt.datetime.fromtimestamp(date).strftime('%Y%m%d%H%M') for date in dates]
print(dates)
给出:
['201706200325', '201706200325', '201706200325', '201706200326', '201706200326']
请参阅此SO answer进行详细说明。