【发布时间】:2018-08-01 22:10:26
【问题描述】:
(注意:我大多是 Python 新手,所以我的代码比优雅的代码更实用,或者我希望它变得实用。)
使用 Python 3.6 和 pygal 2 我正在尝试从存储阵列的 REST API 中读取 json 格式的数据。我想提取延迟值(读取、写入、总计)并创建一个散点图来显示这些值。
我从关于绘制日期的 pygal 文档开始,here。
json 中的时间戳都是以纪元毫秒为单位的。因此,当我阅读它们时,我必须划分它们以使它们与日期时间库一起使用。
示例 json 数据部分:
[
1532908042508,
{
"latency.total": 1.09258,
"partialBlocksRatio.total": 18.58528,
"iops.read": 5984.2,
"latency.read": 1.1011,
"iops.write": 2181.4,
"throughput.read": 1461.03762,
"throughput.write": 105.14331,
"throughput.total": 1566.18092,
"iops.total": 8165.6,
"latency.write": 1.06919
}
],
我的问题是,一旦我得到数据,如何将其输入 pygal。我从定义图表开始(取自 pygal 文档示例)
我提取数据,将纪元毫秒转换为秒,创建所需格式的时间字符串(Y、m、d、H、M、S)并将该列表放入列表中。 接下来将延迟数据加载到列表中。 加载完所有数据后,我添加到 pygal 并尝试渲染到文件。
if key.lower() == 'history':
for key2, historyData in value:
epochSec = key2 / 1000.0 # The key for each history entry is epoch time in milliseconds. Time functions in Python
# appear to want seconds. So the division is needed.
timeStamp = [datetime.fromtimestamp(epochSec).strftime('%Y, %m,
%d, %H, %M, %S')] # Create the list format used for pygal
timeStamps.append(timeStamp)
#print(timeStamp)
for key3 in historyData:
if key3.lower() == 'latency.read':
perfHistory.append(historyData[key3])
for stamp in timeStamps:
#print(stamp[0])
xy_dateLine.add("", datetime(int(stamp[0])),perfHistory)
xy_dateLine.render_to_file('scatter4-1.svc')
错误是 回溯(最近一次通话最后): 文件“C:/Users/../python/XIO_DataPlotting5.py”,第 57 行,在 xy_dateLine.add("", datetime(int(stamp[0])),perfHistory) ValueError: int() 以 10 为基数的无效文字:'2018, 07, 22, 18, 02, 07'
我在这里的感觉是我忽略了一些简单的东西,但我却不知所措。我是否过于复杂了?有没有我的 google-fu 发现的 pygal 教程?
【问题讨论】:
标签: python python-3.x python-datetime pygal