Search code examples
pythonregexpython-3.xnetscaler

Using '\n' to place regex findings on a line for seperation. '+' unsupported operand


So yesterday I got my regex to be printed line for line. Today I removed my if statement, set "result" equal to the regex so I could append it to my list. Well it broke my (dns+'\n'). Maybe related or not. the error that is being displayed to me is the following.

    dns.append(result+'\n')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Code:

import re
import pandas as pd

dns = []
addstrip = []
with open('ns.txt', 'r') as file:
    lines = file.read().splitlines()

    for line in lines:
        #result = (re.search(r'(\W\S+)(\.)(\S+)(\.)(\S\S\S+)', line))
        result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
        dns.append(result+'\n')
        print(dns)
    with open('dnsout.txt', 'w') as f:
        f.writelines(str(dns))

Output with no '\n'

[None]
[None, None]
[None, None, None]
[None, None, None, None]
[None, None, None, None, None]
[None, None, None, None, None, None]

OLD Code:

dns = []
with open('ns.txt', 'r') as file:
    lines = file.read().splitlines()
    for line in lines:
        if re.search(r'^(add lb vserver )(\S+) (\S+) (\S+)(.+)$', line):
            dns.append(line)
            print(dns)
    with open('dnsout.txt', 'w') as f:
        f.writelines(lines)

Later Output After the 'None':

match='add lb vserver SSL_INT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 51), match='add lb vserver SSL_EXT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object; 

In the above code I was not actually printing out the regex findings but rather the whole line. The output was line by line though maybe bc the regex findings are different datatype?

Pandas is being used later on in code below this so you can ignore. How would one go about separating by line? My guess is that I will need to convert it to some other type that can take the +'\n'. Any help, tips, or tricks are appreciated. Thanks for reading.


Solution

  • As per your comments,

    import re
    import pandas as pd
    
    dns = []
    addstrip = []
    
        with open('ns.txt', 'r') as file:
            lines = file.read().splitlines()
    
            for line in lines:
    
                result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
                if result:
                   dns.append(result.group(x)+'\n') # x is the group number that you would need. 
                   print(dns, end=",")
            with open('dnsout.txt', 'w') as f:
              f.writelines(str(dns))