3

我对 Python 和 openstack ceilometer 有点陌生。我正在使用以下代码读取云高仪数据:

import ceilometerclient.client

cclient = ceilometerclient.client.get_client(2, os_username="Ceilometeradmin", os_password="blahblah", os_tenant_name="blahblah", os_auth_url="http://xxx.xx.xx.x:5000/v2.0")

query = [dict(field='resource_id', op='eq', value='dd893564-85e5-43f8-a384-086417f1d82c')]

ls = cclient.meters.list(q=query)

(请参阅附件的输出图片)

在此处输入图像描述

有谁知道我如何将其转换为数据框?

我试过了 :ls2 = pandas.DataFrame(ls, columns=["user_id", "name", "resource_id", "source", "meter_id", "project_id", "type", "unit"])

但得到以下错误:

Traceback (most recent call last):
File "", line 1, in File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 250, in init copy=copy)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 363, in _init_ndarray return create_block_manager_from_blocks([values.T], [columns, index])
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3750, in create_block_manager_from_blocks construction_error(tot_items, blocks[0].shape[1:], axes, e)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3732, in construction_error passed,implied))
ValueError: Shape of passed values is (1, 2), indices imply (8, 2)

如果有人可以提供帮助,那将非常感激..

谢谢

最好的祝愿

4

2 回答 2

1

在同事的帮助下,我们设法找到了解决方案。我发布它以防它对任何试图将云高仪数据转换为数据框的人有用。

ls2 = str(ls)[8:-2]
tmp = ls2.replace("u'","'")
tmp = tmp.replace("<Meter","")
tmp = tmp.replace("}>","}")
tmp= "["+tmp+"]"
tmp = tmp.replace("'", "\"")
parsed = json.loads(tmp)
ls3 = pandas.DataFrame(parsed)

替换了使其成为无效字典并转换为数据框的字符

于 2015-10-30T12:17:18.943 回答
0
meters = c.meters.list()
fields = ['user_id', 'name', 'resource_id', 'source', 'meter_id', 'project_id', 'type', 'unit']
converted_meters = [dict([(key, getattr(m, key, None))]) for m in meters for key in fields]
data_frame = pandas.DataFrame(converted_meters)
于 2015-11-14T13:59:50.857 回答