How to call variable inside a string? - python

I am trying to call a variable inside a string but cannot get it to work. I have tried researching this but cannot find a way to get it to work.
How can I call set_drive in the Drive part of soup = post. :
set_drive = "ON"
soup = post("""
<?xml version="1.0" encoding="UTF-8"?>
<Packet>
<Command>setRequest</Command>
<DatabaseManager>
<Mnet Group="18" Drive=set_drive Mode="COOL" SetTemp="19" AirDirection="HORIZONTAL" FanSpeed="HIGH" />
</DatabaseManager>
</Packet>
""")

The recommanded (and most readable and portable) solution is to use str.format():
set_drive = "ON"
template = """
<?xml version="1.0" encoding="UTF-8"?>
<Packet>
<Command>setRequest</Command>
<DatabaseManager>
<Mnet Group="18" Drive="{set_drive}" Mode="COOL" SetTemp="19" AirDirection="HORIZONTAL" FanSpeed="HIGH" />
</DatabaseManager>
</Packet>
"""
soup = post(template.format(set_drive=set_drive))

Use string formatting. There's many choices in python.
set_drive = "ON"
result = "Drive=%s" % set_drive # traditional way
result = f"Drive={set_drive}" # python 3.6+ way

set_drive = "ON"
query =
"""<?xml version="1.0" encoding="UTF-8"?>
<Packet>
<Command>setRequest</Command>
<DatabaseManager>
<Mnet Group="18" Drive={set_drive} Mode="COOL" SetTemp="19" AirDirection="HORIZONTAL" FanSpeed="HIGH" />
</DatabaseManager>
</Packet>
"""
query.format(set_drive=set_drive)

Related

Remove namespaces and nodes from XML string in python

I get an xml string from a post request and I need to use this xml in a subsequent request. I need to edit the XML from the first request to reflect the correct format for the subsequent request.
I can successfully remove the name spaces but am struggling with extracting the desired node and keeping the xml formatting.
current format
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetExResponse xmlns="http://www.someurl.com/">
<GetExResult>
<DataMap xmlns="" sourceType="0">
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1"/>
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1"/>
</DataMap>
</GetExResult>
</GetExResponse>
</soap:Body>
</soap:Envelope>
Desired Format
<?xml version="1.0" encoding="UTF-8"?>
<DataMap xmlns="" sourceType="0">
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1"/>
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1"/>
</DataMap>
--removes namespaces
dmXML = xmlstring
from lxml import etree
root = etree.fromstring(dmXML)
for elem in root.getiterator():
elem.tag = etree.QName(elem).localname
etree.cleanup_namespaces(root)
test = etree.tostring(root).decode()
print(test)
--extracts desired node but into dataframe changing the formatting
xdf = pandas.read_xml(dmXML, xpath='.//DataMap/*', namespaces={"doc": "http://www.w3.org/2001/XMLSchema"})
xml = pandas.DataFrame.to_xml(xdf)
You can simply extract the relevant portion into a new document:
import xml.etree.ElementTree as ET
root = ET.fromstring(dmXML)
new_root = root.find('.//DataMap')
print(ET.tostring(new_root, xml_declaration=True, encoding='UTF-8').decode())
Output:
<?xml version='1.0' encoding='UTF-8'?>
<DataMap sourceType="0">
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1" />
<FieldMap flag="Q1" destination="Q1_1" source="Q1_1" />
</DataMap>

Python - How to parse xml response and store a elements value in a variable?

I am getting the XML response from the API call.
I need the "testId" attribute value from this response. Please help me on this.
r = requests.get( myconfig.URL_webpagetest + "?url=" + testurl + "&f=xml&k=" + myconfig.apikey_webpagetest )
xmltxt = r.content
print(xmltxt)
testId = XML(xmltxt).find("testId").text
r = requests.get("http://www.webpagetest.org/testStatus.php?f=xml&test=" + testId )
xml response:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<statusCode>200</statusCode>
<statusText>Ok</statusText>
<data>
<testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId>
<ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey>
<jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl>
</data>
</response>
The following error is produced:
Traceback (most recent call last):
File "/pagePerformance.py", line 52, in <module>
testId = XML (xmltxt).find("testId").text
AttributeError: 'NoneType' object has no attribute 'text'
Use the following to collect testId from response:-
import xml.etree.ElementTree as ET
response_xml_as_string = "xml response string from API"
responseXml = ET.fromstring(response_xml_as_string)
testId = responseXml.find('data').find('testId')
print testId.text
from lxml.etree import fromstring
string = '<?xml version="1.0" encoding="UTF-8"?> <response> <statusCode>200</statusCode> <statusText>Ok</statusText> <data><testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId> <ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey> <jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl> </data> </response>'
response = fromstring(string.encode('utf-8'))
elm = response.xpath('/response/data/testId').pop()
testId = elm.text
This way you can search for any element within the xml from the root/parent element via the XPATH.
Side Note: I don't particular like using the pop method to remove the item from a single item list. So if anyone else has a better way to do it please let me know. So far I've consider:
1) elm = next(iter(response.xpath('/response/data/testId')))
2) simply leaving it in a list so it can use as a stararg
I found this article the other day when it appeared on my feed, and it may suit your needs. I skimmed it, but in general the package parses xml data and converts the tags/attributes/values into a dictionary. Additionally, the author points out that it maintains the nesting structure of the xml as well.
https://www.oreilly.com/learning/jxmlease-python-xml-conversion-data-structures
for your use case.
>>> xml = '<?xml version="1.0" encoding="UTF-8"?> <response> <statusCode>200</statusCode> <statusText>Ok</statusText> <data> <testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId> <ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey> <jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl> </data> </response>'
>>> root = jxmlease.parse(xml)
>>> testid = root['response']['data']['testId'].get_cdata()
>>> print(testid)
>>> '180523_YM_054fd7d84fd4ea7aed237f87289e0c7c'

How to parse SOAP XML with Python

I have some SOAP responses saved in a file which I would like to parse,
Part of example file:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns0:GetList_Operation_0Response xmlns:ns0="urn:COMPANY:TEST:Assets" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:getListValues>
<ns0:Status>New</ns0:Status>
<ns0:FormType>Support Group</ns0:FormType>
<ns0:PersonRole>Supported by</ns0:PersonRole>
<ns0:FullName>Data Centre</ns0:FullName>
<ns0:PeopleGroupFormEntryID>SG0003</ns0:PeopleGroupFormEntryID>
<ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWDS</ns0:PeopleGroupInstanceID>
<ns0:AssetClassId>UPS</ns0:AssetClassId>
<ns0:AssetInstanceId>ASDAWDDAWSDWADS66666</ns0:AssetInstanceId>
</ns0:getListValues>
<ns0:getListValues>
<ns0:Status>New</ns0:Status>
<ns0:FormType>Support Group</ns0:FormType>
<ns0:PersonRole>Supported by</ns0:PersonRole>
<ns0:FullName>Unix</ns0:FullName>
<ns0:PeopleGroupFormEntryID>SG0004</ns0:PeopleGroupFormEntryID>
<ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWQQ</ns0:PeopleGroupInstanceID>
<ns0:AssetClassId>COMPUTERSYSTEM</ns0:AssetClassId>
<ns0:AssetInstanceId>ASDAWDDAWSDWADS55555</ns0:AssetInstanceId>
</ns0:getListValues>
</ns0:GetList_Operation_0Response>
</soapenv:Body>
I would like to get (FullName & AssetInstanceId):
Data Centre;ASDAWDDAWSDWADS66666
Unix;ASDAWDDAWSDWADS55555
Could you suggest the best method to do that? Whenever I try to do that with ElementTree I get error of
"SyntaxError: expected path separator (:)"
Probably because of ns0: annex in beginning of every line
Looks like your xml is broken. You can try using regex to get the required values
Demo:
a = """<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns0:GetList_Operation_0Response xmlns:ns0="urn:COMPANY:TEST:Assets" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:getListValues>
<ns0:Status>New</ns0:Status>
<ns0:FormType>Support Group</ns0:FormType>
<ns0:PersonRole>Supported by</ns0:PersonRole>
<ns0:FullName>Data Centre</ns0:FullName>
<ns0:PeopleGroupFormEntryID>SG0003</ns0:PeopleGroupFormEntryID>
<ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWDS</ns0:PeopleGroupInstanceID>
<ns0:AssetClassId>UPS</ns0:AssetClassId>
<ns0:AssetInstanceId>ASDAWDDAWSDWADS66666</ns0:AssetInstanceId>
</ns0:getListValues>
<ns0:getListValues>
<ns0:Status>New</ns0:Status>
<ns0:FormType>Support Group</ns0:FormType>
<ns0:PersonRole>Supported by</ns0:PersonRole>
<ns0:FullName>Unix</ns0:FullName>
<ns0:PeopleGroupFormEntryID>SG0004</ns0:PeopleGroupFormEntryID>
<ns0:PeopleGroupInstanceID>ASDAWDASDWADSDWSDWQQ</ns0:PeopleGroupInstanceID>
<ns0:AssetClassId>COMPUTERSYSTEM</ns0:AssetClassId>
<ns0:AssetInstanceId>ASDAWDDAWSDWADS55555</ns0:AssetInstanceId>
</ns0:getListValues>"""
import re
FullName = re.findall("<ns0:FullName>(.*?)</ns0:FullName>", a)
AssetInstanceId = re.findall("<ns0:AssetInstanceId>(.*?)</ns0:AssetInstanceId>", a)
for i in zip(FullName, AssetInstanceId):
print(i)
Output:
'Data Centre', 'ASDAWDDAWSDWADS66666'
'Unix', 'ASDAWDDAWSDWADS55555'

python, beautiful soup, xml parsing

How can I get values of latitude and longitude from the following XML:
<?xml version="1.0" encoding="utf-8"?>
<location source="FoundByWifi">
<coordinates latitude="49.7926292" longitude="24.0538406"
nlatitude="49.7935180" nlongitude="24.0552174" />
</location>
I tried to use get_text but it doesn't work in this way(
r = requests.get(url)
soup = BeautifulSoup(r.text)
lat = soup.find('coordinates','latitude').get_text(strip=True)
'latitude' is an attribute within the 'coordinates' tag. Once you found the coordinates, the soup object stores all the attributes in a dict-like key-value store.
So, in your case, after finding the coordinates tag, check the 'latitude' key as so:
lat = soup.find('coordinates')['latitude']
You can even strip the resultant of any extraneous whitespace at the beginning or end:
lat = soup.find('coordinates')['latitude'].strip()
Check online demo
html_doc = """
<?xml version="1.0" encoding="utf-8"?>
<location source="FoundByWifi">
<coordinates latitude="49.7926292" longitude="24.0538406"
nlatitude="49.7935180" nlongitude="24.0552174" />
</location>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
lat = soup.find_all('coordinates')
for i in lat:
print(i.attrs['latitude'])
print(i.attrs['longitude'])

XML parsing to get list of values in Python

i have a XML output like below:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getValuesResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://soap.core.green.controlj.com"><getValuesReturn soapenc:arrayType="xsd:string[3]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><getValuesReturn xsi:type="xsd:string">337.81998</getValuesReturn><getValuesReturn xsi:type="xsd:string">129.1</getValuesReturn><getValuesReturn xsi:type="xsd:string">1152.9691</getValuesReturn></getValuesReturn></ns1:getValuesResponse></soapenv:Body></soapenv:Envelope>
I want to get all the values regarding "getValuesReturn" attribute as a Python list. For this, i used a code like below:
import libxml2
DOC="""<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getValuesResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://soap.core.green.controlj.com"><getValuesReturn soapenc:arrayType="xsd:string[3]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><getValuesReturn xsi:type="xsd:string">337.81998</getValuesReturn><getValuesReturn xsi:type="xsd:string">129.1</getValuesReturn><getValuesReturn xsi:type="xsd:string">1152.9691</getValuesReturn></getValuesReturn></ns1:getValuesResponse></soapenv:Body></soapenv:Envelope>"""
def getValues(cat):
return [attr.content for attr in doc.xpathEval("/elements/parent[#name='%s']/child/#value" % (cat))]
# gelen xml dosyasini yazdir
doc = libxml2.parseDoc(DOC)
#getValuesReturn etiketinin degerlerini yazdir
print getValues("getValuesReturn")
It just returns me an empty list. But i should get a list such as ["337.81998","129.1","1152.9691"]. Could you please help me out with this ?
Thanks in advance.
Where does the xpath expression come from? It doesn't match anything. (There's no elements, parent tag element)
Try following:
DOC = ...
doc = libxml2.parseDoc(DOC)
print [attr.content for attr in doc.xpathEval(".//getValuesReturn")]
prints
['337.81998129.11152.9691', '337.81998', '129.1', '1152.9691']
doc = libxml2.parseDoc(DOC)
print [attr.content for attr in doc.xpathEval('.//getValuesReturn/text()')]
prints
['337.81998', '129.1', '1152.9691']

Categories

Resources