python - 如何更改 json 数据的结构并在 python 上添加新字段

标签 python json

我正在尝试我的 json 数据的结构,但我无法理解它。这是我拥有的数据样本

[<OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'ef392c3d-74fa-43fe-87c5-7e117b6d8a09', u'timestamp': u'2015-07-01T15:13:55', u'counter_volume': 0.034999999999999996, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'ubuntu-io', u'flavor.id': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.small.io', u'disk_gb': u'20', u'kernel_id': u'None', u'image.id': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.ram': u'2048', u'host': u'9fa544f5c47569db21d50bc6c0765296316a56bd6baf6b04d705686a', u'flavor.ephemeral': u'0', u'image.name': u'ubuntu-io', u'image_ref_url': u'link': u"[{'href': 'link', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'20', u'root_gb': u'20', u'name': u'instance-000000d3', u'memory_mb': u'2048', u'instance_type': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'vcpus': u'1', u'image_ref': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.links': u"[{'href': 'link', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:13:56.006000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'ca8ea466-2003-11e5-a764-002590e64886', u'counter_type': u'gauge'}>, <OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'0c8b6b26-3340-41e3-ac8b-cc38f15d3570', u'timestamp': u'2015-07-01T15:08:32', u'counter_volume': 5.4399999999999995, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'kalman_instance', u'flavor.id': u'1', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.tiny', u'disk_gb': u'1', u'kernel_id': u'None', u'image.id': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.ram': u'512', u'host': u'25aa71ded460ea9d4bf52e1aac34017691699cb5e4e389704d738bed', u'flavor.ephemeral': u'0', u'image.name': u'cirros', u'image_ref_url': u'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'image.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'1', u'root_gb': u'1', u'name': u'instance-0000013c', u'memory_mb': u'512', u'instance_type': u'1', u'vcpus': u'1', u'image_ref': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/flavors/1', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:08:32.459000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'09b5173e-2003-11e5-ac7a-002590e64b12', u'counter_type': u'gauge'}>]

我想改变这样的事情

 message: {
      columns: [
        ["y": 0.043, "x": "2015-06-30T15:53:55"],
        ["y": 0.045, "x": "2015-06-30T15:53:55"]
      ]

这是我的代码,

def clean_Json(data):
  for each in data:
    timestamp = each.timestamp
    volume =  each.counter_volume
    i = json.dumps({'x': timestamp, 'y': volume})
    print i

clean_Json(data)

结果:

{"y": 4.101666666666667, "x": "2015-04-10T15:18:18"}
{"y": 5.471666666666666, "x": "2015-04-10T14:48:18"}

问题是

1. 每行末尾没有逗号。 2. 当我尝试添加方括号时出现语法错误。

我似乎无法从 json.dumps({'x': timestamp, 'y': volume})

最佳答案

这个怎么样?

result_rows = []
for row in data:
    # Format output as desired
    formatted = """["y": %0.3f, "x": "%s"]""" % (row.counter_volume, row.timestamp)

    #Append to result list
    result_rows.append(formatted)

# Pythonic way to join strings putting something (comma+newline) between items
print(',\n'.join(result_rows))

关于python - 如何更改 json 数据的结构并在 python 上添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31159539/

相关文章:

python - tox 为看似有效的命令抛出 "tox: error: unrecognized arguments:"

python - 为什么我的图像上传到 Django Rest Framework 后无法读取?

ruby - 安装 json gem : The 'json' native gem requires installed build tools 时出错

java - 如何遍历 JSONObject?

python - Elasticsearch延迟存储并立即搜索

python - 关于编写测试友好代码的任何提示?

python peewee 原始查询,不转义字符

json - JSON API 的 XSS 漏洞

php - 创建新的jstree节点后如何设置id?

javascript - 如何从 plist 文件创建 json 文件?