我正在控制一个智能灯泡并在UI仪表板中发布它们的状态,作为一种状态,我得到了一个字符串数据“ON”和“OFF”,但是我不想得到这个字符串数据,而是想要得到一个整数数据1和0,不管怎样,我可以使用Python字典中的JSON模块将这个字符串数据转换成整数数据。你知道吗
我的智能灯泡的一些API代码如下:
if (_deviceUrl.getcode() == 200):
data = _deviceUrl.read().decode("utf-8")
# Use the json module to load the string data into a dictionary
_theJSON = json.loads(data)
# 1. status
devicedata['on'] = self.on_dict[_theJSON["action"]['on']]
# 2. brightness convert to %
devicedata["bri"] = int(round(float(_theJSON["action"]["bri"]) * 100 / 255, 0))
可以使用以下构造:
这是不言自明的。你知道吗
用这样的整数替换字符串的一种方法是定义一个单独的字典:
例如,如果
d['action']['status']
的值是字符串“ON”,那么ints[d['action']['status']]
返回整数1。你知道吗相关问题 更多 >
编程相关推荐