首页 文章

pytr在x轴上绘制日期

提问于
浏览
0

(注意:我在Python中大部分都是新手,所以我的代码比优雅的功能更强大,或者我希望它变得有用 . )

使用Python 3.6和pygal 2我试图从存储阵列的REST API读取json格式化数据 . 我想拉出延迟值(读,写,总)并创建一个显示值的散点图 .

我开始使用有关绘制日期的pygal文档,here .

json中的时间戳都是以纪元毫秒为单位 . 因此,当我阅读它们时,我必须分开使它们与datetime库一起工作 .

示例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 doc示例)

我拉数据,将纪元毫秒转换为秒,将时间字符串创建为所需格式(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')

错误是Traceback(最近一次调用最后一次):文件“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的教程吗?

1 回答

  • 0

    您正在尝试将字符串('2018,07,22,18,02,07')转换为int,因此为valueError .

相关问题