pythonpython-3.xregexconfigparserfileparsing

how to match a multiline section in a file and delete it using regex in Python?


I have an INI file with sections mentioned within [ ] and variables in a key = value manner. In it, I have a section called [FILES] which only has some file (with .ini extension) paths mentioned under it without any key-value pair.

Using configpaser module of Python, I am not able to parse it because of that [FILES] section. Hence, I am trying to get rid of this section from the file by substituting it by blank.

The ini file looks as below, there may or may not be sections defined under [FILES]

[ABC]
p1 = 2 
p2 = 3

[DEF]
q1= 4
q2 = queue

[FILES]
$PWD/../log/inc1-ww.ini
inc2.ini
/home/user/inputs/inc_new.ini
[PQR]
x= xyz
y = 2                                 

I am trying below code, however the section defined after [FILES] is also coming along, any help to correct the regex and delete it from the file will be great help:

import re 
txt = open('my.ini', "r")
data = txt.read()
txt.close()

regex = re.compile(r'^(\[FILES\])[\n\r]([$-_a-zA-Z0-9\/\.\n\r]+)', re.MULTILINE)
matches = [] 
for m in regex.finditer(data):
    matches.append(m.groups())

Thanks in advance!


Solution

  • This is really not worth doing with a regexp; if you can't use ConfigParser even with allow_no_value, read the file and ignore all lines in the FILES section:

    import io
    
    data = io.StringIO("""
    [ABC]
    p1 = 2 
    p2 = 3
    
    [DEF]
    q1= 4
    q2 = queue
    
    [FILES]
    $PWD/../log/inc1-ww.ini
    inc2.ini
    /home/user/inputs/inc_new.ini
    [PQR]
    x= xyz
    y = 2
    """.strip()
    )
    
    current_section = None
    for line in data:
        line = line.rstrip()
        if line.startswith("["):
            current_section = line.strip("[]")
        if current_section != "FILES":
            print(line)
    

    This outputs

    [ABC]      
    p1 = 2     
    p2 = 3     
               
    [DEF]      
    q1= 4      
    q2 = queue 
               
    [PQR]      
    x= xyz     
    y = 2
    

    (You could write to a file or append to a list instead of print()ing the line.)