Getting Started with Python (iii)

Source: Internet
Author: User

I'm going to use Python to aid in parsing the VLAN, Eth-trunk, and interface information in the router configuration, where the relationship between VLANs and interface, Eth-trunk, and interface needs to be reflected.


There are many more structured articles on the web, and the more structured introductory tutorials are basically about the Python language from the very basics. Here I will follow my needs to introduce the introduction of Python, directly through the problem to learn the use of Python.


The first to solve is how to get the name of the device, as described earlier, the name of the device is disp cur command, and then go to "sysname XXX" such a configuration. Therefore, we need to use Python in the configuration file to find "sysname XXX" such a string, after finding the string, "sysname" is the meaning of the configuration, and "XXX" is the name of the device we want.


So, to tidy up the steps:

1. Open the configuration file for the router

2. Read the entire contents of the router into

3, line-by-row lookup "sysname XXX" string (XXX represents the specific name of the device, because a device has only one device name, so as long as the first match to find the content can be)

4. Obtain the name of the device after it is found


Follow the steps in the previous step.

1. Open the configuration file for the router:

filename = ' XXXX.log ' f = file (filename, ' R ')

2. Read all the contents of the router into the

Content = F.readlines ()

3, line search "sysname XXXX"

For C in content:txt = Re.search (' sysname ', c)

4. Get the name of the device and end the search

If txt! = none:print C print C.split () [1] Break


The above is the code for each step that is done with Python, which is briefly described here.

File () is a system function that is used to open a file. To open a file, you need to specify the path and name of the open file, and then specify how you want to open the file, where the file opened is "XXXX.log" and opened with "R", which is read-only to open the file. When the file is opened, a file object is returned, and the file object can be used for file-related operations such as read, write, close, and so on. In the code above, F.readlines () is used to read the contents of the file into the content variable.


When all the files are read into the content variable, use the For loop to iterate through the contents of each line. Then use Re.search () to search for the row for a "sysname" string. Because each device name is not the same, you can only search for the row for "sysname", and if you have one, use Print to output the contents of the line.


In the code for the C in content, each line of content is given in order to the C variable, and then Re.search () is used to find out if there is a "sysname" in the C variable. The search will output print C first, then print C.split () [1].


C.split () [1] What the hell? c is "sysname xxxx", and this is what we need is "XXXX", and do not need "sysname", so use the split () function to split the string by a space, divided into "sysname" and "XXXX" two strings of a list. The first character is [0], and the second string is [1].


Take a look at the following example:

>>> c = ' sysname XXXX ' >>> print c.split () [' sysname ', ' XXXX ']>>> print c.split () [0]sysname >>> print c.split () [1]xxxx>>>


Well, basically that's all there is, the complete code is as follows:

Import refilename = ' XXXX.log ' f = file (filename, ' r ') content = F.readlines () for C in content:txt = Re.search (' sysname ' , c) if txt! = none:print C print C.split () [1] breakf.close ()


The first line of the above code, import re, imports a library, which is a library of regular expressions that are used to match text.


The code is short, but the code contains regular expressions, file operations, looping loops, strings, and so on. This content can be further searched if the reader is not familiar with the relevant information reference.


I did this in order to use the Python language and not to learn the Python language in depth, after all I used the content is not deep, if I really need to grasp and understand the Python language, or first grasp the basic knowledge of the Python language is good.


Note: If there is no basis for Python, then be sure to master the print language, which is used to output the statement, after the above statements do not understand, add a print to observe the value of the currently processed variable. For example, if you need to see content, you can write the following two sentences directly:

For C in Content:print C

In the course of learning to pay attention to this point Oh! Of course, in the process of deep learning, it is best to master the program debugging, so that it will better understand the process of implementation of the program, and eliminate problems in the program.

Getting Started with Python (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.