2

I am making a function that accepts a plain text file and then returns a list of words in that file. Obviously, I would like to get rid of any newline '\n', however, when using '.replace()' nothing happens.

Function:

textfile = 'name.txt'

def read_words(filename):
    f = open(filename,'r')
    message = f.read()
    a = message.replace('\n', '')
    wordlist = a.split(' ')
    print(wordlist)

read_words(textfile)

Sample txt:

This\n\nis\n\n\na\n\n\nmy\n\nwfile with spaces and blanks

My output:

['This\\n\\nis\\n\\n\\na\\n\\n\\nmy\\n\\nwfile', 'with', 'spaces', 'and', 'blanks']

Why is the '.replace()' method not working?

Okeh
  • 163
  • 1
  • 7
  • Please always use a generic [python] tag for python questions – juanpa.arrivillaga Mar 09 '19 at 01:22
  • What isnt working? This code seems fine to me – nathan.medz Mar 09 '19 at 01:24
  • 2
    The `replace` method is certainly working. What's your expected output? – blhsing Mar 09 '19 at 01:25
  • Perhaps this is a problem with my IDE. I use 'Thonny' – Okeh Mar 09 '19 at 01:25
  • New line can be `\n`, `\r\n` or `\r` https://stackoverflow.com/questions/15433188/r-n-r-and-n-what-is-the-difference-between-them. How about using the built-in function `readlines()` instead to read a file and split the content by newline https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list? Afterward, you can do something like `[ line.split() for line in lines ]` – dekauliya Mar 09 '19 at 01:25
  • 2
    _Why is the `'.replace()'` method not working?_ Show us the output that makes you think it isn't working. You've only shown us the sample file contents, which doesn't demonstrate anything. – John Gordon Mar 09 '19 at 01:26
  • Does your sample text contain the *two-character* `\n`? That's the only reason replace wouldn't work. – Mark Tolonen Mar 09 '19 at 01:28
  • 5
    Is sample txt literally the string `This\n\nis\n\n\na\n\n\nmy\n\nwfile with spaces and blanks`, or are you replacing the newlines with `\n` for us? If the former, you'll need to escape the backslash in your replace: `message.replace('\\n', '')` – Dillon Davis Mar 09 '19 at 01:28
  • 1
    How does one input the single character '\n' in a txt file? – Okeh Mar 09 '19 at 01:29
  • 1
    Hit enter. The `\n` escape in a string literal stands for a line break character. – user2357112 Mar 09 '19 at 01:30
  • @Okeh Worked well and fine for me (Spyder & PyCharm IDEs). Got the following output `['Thisisamywfile', 'with', 'spaces', 'and', 'blanks']` – Sagar Mar 09 '19 at 01:35
  • 1
    I have been misled in thinking the two character '\n' is the same as the one character '\n'. Thanks for the clarification! – Okeh Mar 09 '19 at 01:38
  • I love this thread. No downvotes, no snarkiness, just help for a new programmer learning the ropes. – broAhmed Mar 09 '19 at 01:41
  • 2
    @DillonDavis Happy to accept an answer of yours if you post one – Okeh Mar 09 '19 at 01:45
  • @Okeh does `.split()` not work? – Lord Elrond Mar 09 '19 at 01:46
  • Why do not you add one line to the function to see what exactly is the content of the variable? Add one more debug line there `print("{} --- {}".format(message,type(message)))`. The `.split("\n", "")` method works. The error is in the contents of the variable `message`. Try this: `message.replace("\n","").replace("\r","")` – s3n0 Mar 09 '19 at 02:05
  • Oh sorry, the problem is solved, I did not notice. :) Then I can only advise this: `message.replace("\\n","").replace("\n","")` – s3n0 Mar 09 '19 at 02:17

2 Answers2

1

This might be case that python or some other programming languages reads new line as '\n' escape character. So when python reads your file '\n' means new line and '\\n' means actual '\n' character you wrote in the text file.

So you need to replace like a = message.replace('\\n', '')

dxillar
  • 307
  • 2
  • 9
1

The issue with your current text replacement is that \ is considered an escape character- the literal characters \n are interpreted as a newline character instead. To solution to this is to escape the \ character itself, via \\. Your updated replace statement would then read:

a = message.replace('\\n', '')

instead of:

a = message.replace('\n', '')
Dillon Davis
  • 6,679
  • 2
  • 15
  • 37