Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete some subitems in a configuration file in PYTHON

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In order to solve the problem of how to delete some subitems in the configuration file in PYTHON, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find an easier way.

Python2.7

Description:

Delete some subitems in multiple configuration files (delete the configuration items in the 192.168.2.xxx section). The configuration file format is as follows:

Define host {

Use linux-server

Host_name TM_2_202

Alias 202

Address 192.168.2.202

}

Define host {

Use linux-db

Host_name TM_2_187

Alias 187

Address 192.168.2.187

}

Define host {

Use linux-db

Host_name TM_22_189

Alias 189

Address 192.168.22.189

}

The result after the subitem that meets the criteria is deleted is as follows:

Click (here) to collapse or open

Define host {

Use linux-db

Host_name TM_22_189

Alias 189

Address 192.168.22.189

}

The implementation is as follows:

Click (here) to collapse or open

#! / usr/bin/env python

# coding: utf-8

Import os

Import re

Def create_new_cfg (old_path,new_path):

For filename in os.listdir (old_path):

If os.path.isfile (filename) and re.search ('^ tm_',filename) and re.search ('hosts.cfg',filename):

Old_fi = open (filename,'r')

New_fi = open (os.path.join (new_path,'new_'+filename),'w')

Li= []

For line in old_fi.readlines ():

If line.split ():

Li.append (line)

If re.search ('}', line):

If re.search ('_ 2mom _ 1 li [2]):

Li = []

Continue

For s in li:

New_fi.write (s)

Li = []

Old_fi.close ()

New_fi.close ()

If _ _ name__ = ='_ _ main__':

Create_new_cfg ('. /','/ tmp')

Script parsing:

① line.split () determines whether it is a blank line (the delimiter such as'\ r'\ n'\ t' will return an empty list)

② re.search ('}', line) indicates that when a row contains'}', it means that at the end of the subitem (which already exists in the li list), further judgment is being made.

③ in which os.path.isfile, os.listdir and os.path.join methods are all related operations on the file system

Expand:

The method of judging blank lines by ①

1 > line.split () all spaces, tabs and carriage returns are used as delimiters and empty strings are filtered out.

2 > line.strip () =''delete the'\ t''\ n'\ r''in the head and tail of the line

Regular matching of ② python

1 > re.search (pattern,str) looks for a pattern match within the string until the first match is found and then returns, or None if the string does not match.

2 > re.match (pattern,str) matches a pattern from the beginning of the string. If the beginning of the string does not conform to the regular expression, the match fails and the function returns None.

This is the answer to the question about how to delete some subitems in the configuration file in PYTHON. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report