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

时间:2016-08-30 08:23:48

标签: python

这是我在这里的第一篇文章,如果我的帖子不遵循"标准"你知道为什么。 而且我真的很喜欢python和编程,我正在努力学习。

我正在使用一个控制我的Husqvarna Automower

的脚本

在那个剧本中有一条我不理解的线,我想改变它的结果。

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

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

{u'storedTimestamp': u'1472541846629', u'hdop': u'0.0', u'latitude': u'57.57320833333333', u'lastErrorCode': u'0', u'nextStartTimestamp': u'1472587200', u'mowerStatus': u'PARKED_TIMER', u'cachedSettingsUUID': u'c1029c29-ecd5-48bd-a27b-fa98c6985ff0', u'hostMessage': u'0', u'configChangeCounter': u'846', u'longitude': u'12.04773', u'nextStartSource': u'WEEK_TIMER', u'secondsOld': u'-1471069304597', u'gpsStatus': u'USING_GPS_MAP', u'gsmRssi': u'0', u'batteryPercent': u'100', u'connected': u'true', u'operatingMode': u'AUTO', u'lastErrorCodeTimestamp': u'0'}

我知道这一行执行" status"功能和打印结果,但我真的不明白字典和['割草机信息']以及为什么我找不到任何参考['割草机信息']脚本中的其他任何东西。据我所知,脚本中应该有一个dictonary。但是我找不到它。

现在要指责这个问题

对print命令进行了保证,希望得到解析后的信息的som信息。

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

该脚本由名为Indigodomo的smarthomesolution运行,并使用python 2.6在mac上破坏

任何人都知道如何做到这一点?

我已从原来的

修改了脚本

这是我修改后的脚本(我的凭据XX出来了)

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)

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

https://github.com/chrisz/pyhusmow

Thanx Martin

1 个答案:

答案 0 :(得分:2)

dic_info = dict(mow.status()['mowerInfo'])
mowerStatus = dic_info.get('mowerStatus')
batteryPercent = dic_info.get('batteryPercent')
相关问题