【发布时间】:2017-11-27 16:07:37
【问题描述】:
我有一个 JSON 文件,其中包含字符串形式的 unix 时间戳。我正在尝试将这些时间戳转换为人类可读的时间,然后在 matplotlib.pyplot 中显示。
在转换时间戳时出现错误:
ValueError:未转换的数据仍然存在:.1806107
如何将我的日期转换为人类可读的形式YY-MM-DD HH-MM-SS?
contents = json.loads(open("foo.json").read())
dates = [ ticker['date'] for ticker in contents ]
data = [ ticker['last'] for ticker in contents ]
# Example of array contents
#dates = [1497918349.3060017, 1497918352.9935713, 1497918358.8218484, 1497918364.0406654, 1497918368.9628277]
#data = [1,2,3,4,5]
# Error occurs here
dates=[dt.datetime.strptime(str(date),'%Y%m%d%H%M') for date in dates]
# Also tried the following but I get the error:
# TypeError: an integer is required (got type datetime.datetime)
dates=[dt.datetime.fromtimestamp(date) for date in dates]
plt.plot(dates, data, 'r-')
plt.show()
【问题讨论】:
标签: python json datetime matplotlib