1

I have a text file that has the following output after pulling information from a device:

DeviceName\n ModelName\n ModelVersion\n 

I'm trying to remove the leading '\n' after each output and put in spacing between each of the specifications above but to no success. My code is as follows:

f = open(FileName, 'r') filedata = f.read() nremove = filedata.replace('\n', ' ') f = open(FileName, 'w') f.write(nremove) f.close() 

This code works for other strings I've removed in the same text file successfully, but when trying to remove the '\n', I get no success.

How can this be altered to remove from the text file?

1 Answer 1

3

you need to escape '\n'. just change nremove = filedata.replace('\n', ' ') to nremove = filedata.replace('\\n', ' ') . it's ok.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.