I am trying to remove the \n characters from the output of a nslookup. While this seems like a simple task and there are many posts about filtering \n they do not seem to work for me. Code works perfectly in Python 2.7 however it does not in 3.6
mx = str(subprocess.check_output(('nslookup -type=mx ' + "test.nl"), shell=True))
clean = re.sub(r"\n", " ", mx)
clean2 = mx.replace("\n", " ")
print(clean)
print(clean2)
Output
\nNon-authoritative answer:\ntest.nl\tmail exchanger
Wanted result
Non-authoritative answer: test.nl\tmail exchanger
Neither of these work, however when I remove the '\' it filters the 'n' perfectly. Can anyone tell me what I am doing wrong? Or how I can achieve the wanted result?
ps: Well aware this could be a duplicate and I'm just making a dumb mistake, However the solutions given in similar posts do not work for me.
When you use \n for replacing, that is interpreted as a line break by python or as line feed character by Regex.
You can use a raw string so Python won't parse the \n:
clean = mx.replace(r"\n", " ")
Or you can escape the symbol:
clean = mx.replace("\\n", " ")