Python split up data from print string
<p>This is my first post here, if my post dont follow the "standard" you know why.
And iam realy new to python and programming, i am trying to learn as i go.</p>

<p>I am using a script thats Controls my Husqvarna Automower</p>

<p>In that script there is a line that i dont understand and i would like to change the outcome of.</p>

<pre><code>print(dict(mow.status()['mowerInfo']))
</code></pre>

<p>When i run the script i get an print out like this</p>

<pre><code>{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'}
</code></pre>

<p>I understands that this line executes the "status" function and prints the outcome, but i dont realy understand the dict and the ['mowerInfo'] and why i cant find any referens to ['mowerInfo'] anywere else in the script. As i understand there should be a dictonary in the script. But i cant find it.</p>

<p>And now to accual the question</p>

<p>Insteed of the print command, a would like to get som of the information parsed inte variables insteed.</p>

<p>For examples would i like to have a variable called mowerStatus and it should have the value PARKED_TIMER and a variable called batteryPercent and it should have the value 100</p>

<p>The script is runned by a smarthomesolution called Indigodomo and is runed on a mac using python 2.6</p>

<p>Anyone knows how to do that?</p>

<p>I have modified the script from the original</p>

<p>here is my modified script (with my credential XXed out)</p>

<pre><code>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 = ("&lt;login&gt;"
                   "  &lt;email&gt;%s&lt;/email&gt;"
                   "  &lt;password&gt;%s&lt;/password&gt;&lt;language&gt;fr-FR&lt;/language&gt;"
                   "&lt;/login&gt;") % (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 = ("&lt;settings&gt;"
                   "    &lt;autoTimer/&gt;&lt;gpsSettings/&gt;&lt;drivePastWire/&gt;"
                   "    &lt;followWireOut&gt;&lt;startPositionId&gt;1&lt;/startPositionId&gt;&lt;/followWireOut&gt;"
                   "    &lt;followWireOut&gt;&lt;startPositionId&gt;2&lt;/startPositionId&gt;&lt;/followWireOut&gt;"
                   "    &lt;followWireOut&gt;&lt;startPositionId&gt;3&lt;/startPositionId&gt;&lt;/followWireOut&gt;"
                   "    &lt;followWireOut&gt;&lt;startPositionId&gt;4&lt;/startPositionId&gt;&lt;/followWireOut&gt;"
                   "    &lt;followWireOut&gt;&lt;startPositionId&gt;5&lt;/startPositionId&gt;&lt;/followWireOut&gt;"
                   "    &lt;followWireIn&gt;&lt;loopWire&gt;RIGHT_BOUNDARY_WIRE&lt;/loopWire&gt;&lt;/followWireIn&gt;"
                   "    &lt;followWireIn&gt;&lt;loopWire&gt;GUIDE_1&lt;/loopWire&gt;&lt;/followWireIn&gt;"
                   "    &lt;followWireIn&gt;&lt;loopWire&gt;GUIDE_2&lt;/loopWire&gt;&lt;/followWireIn&gt;"
                   "    &lt;followWireIn&gt;&lt;loopWire&gt;GUIDE_3&lt;/loopWire&gt;&lt;/followWireIn&gt;"
                   "    &lt;csRange/&gt;"
                   "    &lt;corridor&gt;&lt;loopWire&gt;RIGHT_BOUNDARY_WIRE&lt;/loopWire&gt;&lt;/corridor&gt;"
                   "    &lt;corridor&gt;&lt;loopWire&gt;GUIDE_1&lt;/loopWire&gt;&lt;/corridor&gt;"
                   "    &lt;corridor&gt;&lt;loopWire&gt;GUIDE_2&lt;/loopWire&gt;&lt;/corridor&gt;"
                   "    &lt;corridor&gt;&lt;loopWire&gt;GUIDE_3&lt;/loopWire&gt;&lt;/corridor&gt;"
                   "    &lt;exitAngles/&gt;&lt;subareaSettings/&gt;"
                   "&lt;/settings&gt;")
        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 = ("&lt;control&gt;"
                   "   &lt;action&gt;%s&lt;/action&gt;"
                   "&lt;/control&gt;") % 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&amp;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&amp;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 &gt; 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)
</code></pre>

<p>The orgiginal Project and script can bee find here</p>

<p><a href="https://github.com/chrisz/pyhusmow" rel="nofollow">https://github.com/chrisz/pyhusmow</a></p>

<p>Thanx Martin</p>

<pre><code>dic_info = dict(mow.status()['mowerInfo'])
mowerStatus = dic_info.get('mowerStatus')
batteryPercent = dic_info.get('batteryPercent')
</code></pre>

python