Python从打印字符串中拆分数据

2024-04-25 07:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在这里的第一篇文章,如果我的文章没有遵循“标准”,你知道为什么。 我对python和编程非常陌生,我正在努力学习。在

我用的是一个控制我的Husqvarna自动割草机的脚本

在那个脚本中有一行我不明白,我想改变的结果。在

print(dict(mow.status()['mowerInfo']))

当我运行脚本时,我得到了这样的打印结果

^{pr2}$

我知道这一行执行“status”函数并打印结果,但我不太理解dict和['mowerInfo']以及为什么在脚本中找不到任何引用['mowerInfo']的其他内容。据我所知,剧本里应该有口述。但我找不到。在

现在来指责这个问题

不需要print命令,a希望获得解析的inte变量的som信息。在

例如,我想要一个名为mowerStatus的变量,它应该有值PARKED_TIMER和一个名为batteryPercent的变量,它应该有值100

该脚本由名为Indigodomo的smarthomesolution运行,并在mac上使用python2.6运行

有人知道怎么做吗?在

我把原稿改了

这是我修改过的脚本(我的凭证被去掉了)

import requests
import xmltodict

class API:
    _API_IM = 'https://tracker-id-ws.husqvarna.net/imservice/rest/'
    _API_TRACK = 'https://tracker-api-ws.husqvarna.net/services/'

    def __init__(self):
        self.session = requests.Session()
        self.device_id = None
        self.push_id = None

    def login(self, login, password):
        request = ("<login>"
                   "  <email>%s</email>"
                   "  <password>%s</password><language>fr-FR</language>"
                   "</login>") % (login, password)
            response = self.session.post(self._API_IM + 'im/login',
                                         data=request, headers={'Content type': 'application/xml'})
            response.raise_for_status()

            self.session.headers.update({'Session-Token': response.headers['Session-Token']})

            self.select_first_robot()

    def logout(self):
        response = self.session.post(self._API_IM + 'im/logout')
        response.raise_for_status()
        self.device_id = None
        del (self.session.headers['Session-Token'])

    def list_robots(self):
        response = self.session.get(self._API_TRACK + 'pairedRobots_v2')
        response.raise_for_status()

        result = xmltodict.parse(response.content)
        return result

    def select_first_robot(self):
        result = self.list_robots()
        self.device_id = result['robots']['robot']['deviceId']

    def status(self):
        response = self.session.get(self._API_TRACK + 'robot/%s/status_v2/' % self.device_id)
        response.raise_for_status()

        result = xmltodict.parse(response.content)
        return result

    def geo_status(self):
        response = self.session.get(self._API_TRACK + 'robot/%s/geoStatus/' % self.device_id)
        response.raise_for_status()

        result = xmltodict.parse(response.content)
        return result

    def get_mower_settings(self):
        request = ("<settings>"
                   "    <autoTimer/><gpsSettings/><drivePastWire/>"
                   "    <followWireOut><startPositionId>1</startPositionId></followWireOut>"
                   "    <followWireOut><startPositionId>2</startPositionId></followWireOut>"
                   "    <followWireOut><startPositionId>3</startPositionId></followWireOut>"
                   "    <followWireOut><startPositionId>4</startPositionId></followWireOut>"
                   "    <followWireOut><startPositionId>5</startPositionId></followWireOut>"
                   "    <followWireIn><loopWire>RIGHT_BOUNDARY_WIRE</loopWire></followWireIn>"
                   "    <followWireIn><loopWire>GUIDE_1</loopWire></followWireIn>"
                   "    <followWireIn><loopWire>GUIDE_2</loopWire></followWireIn>"
                   "    <followWireIn><loopWire>GUIDE_3</loopWire></followWireIn>"
                   "    <csRange/>"
                   "    <corridor><loopWire>RIGHT_BOUNDARY_WIRE</loopWire></corridor>"
                   "    <corridor><loopWire>GUIDE_1</loopWire></corridor>"
                   "    <corridor><loopWire>GUIDE_2</loopWire></corridor>"
                   "    <corridor><loopWire>GUIDE_3</loopWire></corridor>"
                   "    <exitAngles/><subareaSettings/>"
                   "</settings>")
        response = self.session.post(self._API_TRACK + 'robot/%s/settings/' % self.device_id,
                                     data=request, headers={'Content-type': 'application/xml'})
        response.raise_for_status()

        result = xmltodict.parse(response.content)
        return result

    def settingsUUID(self):
        response = self.session.get(self._API_TRACK + 'robot/%s/settingsUUID/' % self.device_id)
        response.raise_for_status()

        result = xmltodict.parse(response.content)
        return result

    def control(self, command):
        if command not in ['PARK', 'STOP', 'START']:
            raise Exception("Unknown command")

        request = ("<control>"
                   "   <action>%s</action>"
                   "</control>") % command

        response = self.session.put(self._API_TRACK + 'robot/%s/control/' % self.device_id,
                                    data=request, headers={'Content-type': 'application/xml'})
        response.raise_for_status()

    def add_push_id(self, id):
        request = "id=%s&platform=iOS" % id
        response = self.session.post(self._API_TRACK + 'addPushId', data=request,
                                     headers={'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
        response.raise_for_status()
        self.push_id = id

    def remove_push_id(self):
        request = "id=%s&platform=iOS" % id
        response = self.session.post(self._API_TRACK + 'removePushId', data=request,
                                     headers={'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
        response.raise_for_status()
        self.push_id = None

if __name__ == '__main__':

    retry = 5

    while retry > 0:
        try:
            mow = API()

            mow.login("xxx@xxx.com", "xxxxxx")

            print(dict(mow.status()['mowerInfo']))

            retry = 0
        except Exception as ex:
            retry -= 1
            if retry == 0:
                print("[ERROR] Retrying to send the command")
            else:
                print("[ERROR] Failed to send the command")
                exit(1)

    print("Done")

    mow.logout()

    exit(0)

原始项目和脚本可以在这里找到

https://github.com/chrisz/pyhusmow

萨克斯马丁


Tags: selfapiidforresponserequestsessiondef
1条回答
网友
1楼 · 发布于 2024-04-25 07:30:03
dic_info = dict(mow.status()['mowerInfo'])
mowerStatus = dic_info.get('mowerStatus')
batteryPercent = dic_info.get('batteryPercent')

相关问题 更多 >