AttributeError: 'unicode' object has no attribute 'Bytes_Written'

AttributeError: 'unicode' object has no attribute 'Bytes_Written'

我正在尝试查询服务器,代码非常简单,但我不明白为什么会出现此错误。我正在查询字节读取字节写入和 IO 操作,因为我想稍后在折线图中显示它们。

数据

{
  "Number of Devices": 2,
  "Block Devices": {
    "bdev0": {
      "Backend_Device_Path": "/dev/disk/by-path/ip-192.168.26.1:3260-iscsi-iqn.2010-10.org.openstack:volume-d1c8e7c6-8c77-444c-9a93-8b56fa1e37f2-lun-010.0.0.142",
      "Capacity": "2147483648",
      "Guest_Device_Name": "vdb",
      "IO_Operations": "97069",
      "Bytes_Written": "34410496",
      "Bytes_Read": "363172864"
    },
    "bdev1": {
      "Backend_Device_Path": "/dev/disk/by-path/ip-192.168.26.1:3260-iscsi-iqn.2010-10.org.openstack:volume-b27110f9-41ba-4bc6-b97c-b5dde23af1f9-lun-010.0.0.146",
      "Capacity": "2147483648",
      "Guest_Device_Name": "vdb",
      "IO_Operations": "93",
      "Bytes_Written": "0",
      "Bytes_Read": "380928"
    }
  }
}

代码

#Get data checkpoint size
url = 'url'
r = requests.get(url)
data = r.text
print data


def counterVolume(data):
  for each in data:
        x = each.Bytes_Written
        y = each.Bytes_Read
        z = each.IO_Operations
        print {'Bytes_written': x, 'Bytes_Read': y, 'IO_Operations': z}

我希望输出为

{'Bytes_written': value, 'Bytes_Read': value, 'IO_Operations': value}

假设您有一个有效的 JSON 响应,请按如下方式检索您的数据:

data = r.json()

然后遍历块设备:

for devname, stats in data['Block Devices'].iteritems():
    print '{} had {Bytes_Read} read and {Bytes_Written} written'.format(devname, **stats)

您可以打印为 JSON 响应的子集,使用:

import json

wanted = {'Bytes_Written', 'Bytes_Written', 'IO_Operation'}
for d in data['Block Devices'].itervalues():
    values = {k: v for k, v in d.iteritems() if k in wanted}
    print json.dumps(values)