Search code examples
pythonreplacequotesdouble-quotes

Adding quotes to text in line using Python


I am using Visual Studio Code to replace text with Python. I am using a source file with original text and converting it into a new file with new text.

I would like to add quotes to the new text that follows. For example:

Original text: set vlans xxx vlan-id xxx

New text: vlan xxx name "xxx" (add quotes to the remaining portion of the line as seen here)

Here is my code:

    with open("SanitizedFinal_E4300.txt", "rt") as fin:
        with open("output6.txt", "wt") as fout:
            for line in fin:
                     line = line.replace('set vlans', 'vlan').replace('vlan-id', 'name')
                     fout.write(line)

Is there a way to add quotes for text in the line that follows 'name'?

Edit:

I tried this code:

    with open("SanitizedFinal_E4300.txt", "rt") as fin:
    with open("output6.txt", "wt") as fout:
    for line in fin:
     line = line.replace('set vlans', 'vlan').replace('vlan-id', 'name')
     words = line.split()
        words[-1] = '"' + words[-1] + '"'
        line = ' '.join(words)
     fout.write(line)

and received this error:

line 124, in <module>
words[-1] = '"' + words[-1] + '"'
IndexError: list index out of range

I also tried this code with no success:

    with open("SanitizedFinal_E4300.txt", "rt") as fin:
    with open("output6.txt", "wt") as fout:
    for line in fin:
    line = line.replace('set vlans', 'vlan').replace('vlan-id', 'name')
    
    import re
        t = 'set vlans xxx  vlan-id xxx'
        re.sub(r'set vlans(.*)vlan-id (.*)', r'vlan\1names "\2"', t)
        'vlan xxx  names "xxx"'

Again, my goal is to automatically add double quotes to the characters (vlan numbers) at the end of a line.

For example:

Original text: set protocols mstp configuration-name Building 2021.Rm402.access.mstp.zzz

Desired text: set protocols mstp configuration-name "Building 2021.Rm402.access.mstp.zzz"


Solution

  • Use the following regular expression:

    >>> import re
    >>> t = 'set vlans xxx  vlan-id xxx'
    >>> re.sub(r'set vlans(.*)vlan-id (.*)', r'vlan\1names "\2"', t)
    'vlan xxx  names "xxx"'
    

    The parentheses in the search pattern (first parameter) are used to create groups that can be used in the replacement pattern (second parameter). So the first (.*) match in the search pattern will be included in the replacement pattern by means of \1; same thing goes with the second one.

    Edit: The code I shared is just an example of how to use regular expressions. Here's how you should use it.

    import re
    
    # whatever imports and code you have down to...
    
    with open("SanitizedFinal_E4300.txt", "rt") as fin, open("output6.txt", "wt") as fout:
        for line in fin:
            line = re.sub(r'set vlans(.*)vlan-id (.*)', r'vlan\1names "\2"', line)
            fout.write(line)
    

    IMPORTANT: if the format of the lines you need to modify is any different from the original text example you shared, you'll need to make adjustments to the regular expression.