CiscoDevNet / netprog_basics

Code, Examples, and Resources for the Network Programmability Basics Video Course
MIT License
741 stars 454 forks source link

xmltodict help please Python Part 3 #47

Closed lowfell closed 3 years ago

lowfell commented 3 years ago

Hello all I'm a total newby so bear with me. i'm trying to do the xmltodict example in Useful Python Libraries for Network Engineers

I kind of get how it works and i ran a query myself to get the ip address so here is the xml that he uses

pprint(xml_example) ('<?xml version="1.0" encoding="UTF-8" ?>\n' '\n' ' GigabitEthernet2\n' ' Wide Area Network\n' ' true\n' ' \n' '

\n' ' 172.16.0.2\n' ' 255.255.255.0\n' '
\n' ' \n' '\n')

**Here is the query to generate an ip address notice how it is in the order of the info top down from the xml

test_address = xml_dict["interface"]["ipv4"]["address"]["ip"]**

Here is the query ran

test_address '172.16.0.2'

This only returns the ip address. How would i make the query return the ip address and the netmask ie 172.16.0.2 255.255.255.0 ?

I've tried lots of combinations the nearest i could get to it is

test8 = xml_dict["interface"]["ipv4"]["address"] test8 OrderedDict([('ip', '172.16.0.2'), ('netmask', '255.255.255.0')])

how do i get it with just 172.16.0.2 255.255.255.0 ?

lowfell commented 3 years ago

Hello. Can anyone help here?

annegentle commented 3 years ago

Hi @lowfell -- I think you're working with the XML file at https://github.com/hpreston/python_networking/blob/master/data_manipulation/xml/xml_example.xml -- if so, then you need to get the next-nested line of data from line 9: <netmask>255.255.255.0</netmask>

The "OrderedDict" that Python sees still contains <ip>172.16.0.2</ip><netmask>255.255.255.0</netmask> and what you want to do is get xml_dict to give you the netmask instead of the ip within address.

So you can replace ["ip'] with ["netmask"] to match the XML data as it's given:

test_address = xml_dict["interface"]["ipv4"]["address"]["ip"]
test_netmask = xml_dict["interface"]["ipv4"]["address"]["netmask"]

print("test_address is: " + test_address)
print("test_netmask is: " + test_netmask)

Let me know if that makes sense.

lowfell commented 3 years ago

Hi @lowfell -- I think you're working with the XML file at https://github.com/hpreston/python_networking/blob/master/data_manipulation/xml/xml_example.xml -- if so, then you need to get the next-nested line of data from line 9: <netmask>255.255.255.0</netmask>

The "OrderedDict" that Python sees still contains <ip>172.16.0.2</ip><netmask>255.255.255.0</netmask> and what you want to do is get xml_dict to give you the netmask instead of the ip within address.

So you can replace ["ip'] with ["netmask"] to match the XML data as it's given:

test_address = xml_dict["interface"]["ipv4"]["address"]["ip"]
test_netmask = xml_dict["interface"]["ipv4"]["address"]["netmask"]

print("test_address is: " + test_address)
print("test_netmask is: " + test_netmask)

Let me know if that makes sense.

Hello and thanks again for your help. I'm maybe explaining myself wrong. what I'm after is to create a query from the xml file using _whatevername that will return the ip address AND the subnet mask in one query so if i type in _whatevername 172.16.0.2 255.255.255.0

In this leasson it only returns ONE piece of information like the ip address OR the subnet mask

Can i do this using the same type of logic in this lesson, as that would be more beneficial from the point of view of a network engineeer, if you know what I mean?

annegentle commented 3 years ago

The data you are handed is the pattern you must work with. So yes, you can make address_netmask (or "whatever_name") your new variable by making the concatenation of those two variables test_address plus test_netmask and converting to a string and adding a space when you want to print it exactly as you want it:

address_netmask = str(xml_dict["interface"]["ipv4"]["address"]["ip"]) + " " + xml_dict["interface"]["ipv4"]["address"]["netmask"]
print(address_netmask)

But maybe I'm misunderstanding your question -- I'm explaining how to get a string instead of a dictionary, with a one-liner, but you might need something else in your context so feel free to keep asking questions. 🔬

lowfell commented 3 years ago

So whilst you've given me a string which gives me what i want this isn't like you say not from a dictionary? so, the way i was tring it the nearest I got to it was test8 = xml_dict["interface"]["ipv4"]["address"] test8 OrderedDict([('ip', '172.16.0.2'), ('netmask', '255.255.255.0')])

whilst this returned the data i wanted which was 172.16.0.2 255.255.255.0 it also returned all the stuff i dont really want like all the brackets and the names OrderedDict, ip and netmask ? so are we saying there is no way round this?

Also From a github point of view am i ok to be asking these questions and if i am am i asking in the right place? If im not supposed to be asking questions where can i go for help?

Thanks again

annegentle commented 3 years ago

I would definitely point you to the DevNet community forums and chatrooms on Webex -- click through the options on this page: https://developer.cisco.com/site/support/.

We use GitHub Issue comments to make sure our content and code examples are accurate so it's also okay to double-check if examples aren't working as expected. But yes, in this case, the example is working as expected and you can get more and better discussion and support through the usual DevNet Support channels.

lowfell commented 3 years ago

Apologies and thanks again for your help and patience. I'll be asking my queries on the community now. Brian

Sent from Workspace ONE Boxer

On 9 Jan 2021 02:41, Anne Gentle notifications@github.com wrote:

I would definitely point you to the DevNet community forums and chatrooms on Webex -- click through the options on this page: https://developer.cisco.com/site/support/.

We use GitHub Issue comments to make sure our content and code examples are accurate so it's also okay to double-check if examples aren't working as expected. But yes, in this case, the example is working as expected and you can get more and better discussion and support through the usual DevNet Support channels.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/CiscoDevNet/netprog_basics/issues/47#issuecomment-757082332, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASB3BAWZTN56W644DPBYL6TSY663TANCNFSM4VZJVUTA.

Save Paper - Do you really need to print this e-mail?

Visit www.virginmedia.com for more information, and more fun.

This email and any attachments are or may be confidential and legally privileged and are sent solely for the attention of the addressee(s).

Virgin Media will never ask for account or financial information via email. If you are in receipt of a suspicious email, please report to www.virginmedia.com/netreport

If you have received this email in error, please delete it from your system: its use, disclosure or copying is unauthorised. Statements and opinions expressed in this email may not represent those of Virgin Media. Any representations or commitments in this email are subject to contract.

Registered office: 500 Brook Drive, Reading, RG2 6UU

Registered in England and Wales with number 2591237