我正在尝试查询服务器,代码非常简单,但我不明白为什么会出现此错误。我正在查询字节读取、字节写入和 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()
然后迭代 block 设备:
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)
关于python - 属性错误: 'unicode' object has no attribute 'Bytes_Written' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083828/