java - Python: xml.etree.ElementTree.ParseError: not well-formed (invalid token) -
i have given sample code(which contains xml variable) , want read of attributes of xml , update them. once updated want post them using requests.post. error "not formed" token, , not able parse xml. please suggest wrong in code.
# -*- coding: utf-8 -*- xml.etree import elementtree etree dataxml = """<apidatamessage messageid="747950743" sensorid=extref messagedate=messagedate state="16" signalstrength=random.randint(40,70) voltage="2.83" battery=random.randint(80,90) data=random.randint(27,40) displaydata="67.1° f" plotvalue="67.1" metnotificationrequirements="false" gatewayid="106558" datavalues="19.5" datatypes="temperaturedata" plotvalues="67.1" plotlabels="fahrenheit" />""" parser = etree.xmlparser(encoding="utf-8") root = etree.fromstring(dataxml, parser=parser) root.set('signalstrength',100) print etree.tostring(root)
according me following need in code:
- xml attribute value must provided
""
e.g.<test id="12">
. missng in inputsensorid=extref messagedate=messagedate
- evaluate value of random first , add value string create complete tag.
- need string in
set
method i.e.root.set('signalstrength','100')
demo:
dataxml = """<apidatamessage messageid="747950743" sensorid="extref"\ messagedate="messagedate" state="16" signalstrength="%s" \ voltage="2.83" battery="%s" data="%s" displaydata="67.1° f" plotvalue="67.1" \ metnotificationrequirements="false" gatewayid="106558" datavalues="19.5" \ datatypes="temperaturedata" plotvalues="67.1" plotlabels="fahrenheit" />"""\ %(random.randint(40,70), random.randint(80,90), random.randint(27,40)) xml.etree import elementtree etree parser = etree.xmlparser(encoding="utf-8") root = etree.fromstring(dataxml, parser=parser) root.set('signalstrength',"100") print etree.tostring(root)
Comments
Post a Comment