"\n" in strings not working
Note that if you want to support Python-style strings (with not only \n
but also \t
, \r
, \u1234
, etc.), you should use codecs.decode
with the unicode_escape
handler:
contents = input()
contents = codecs.decode(contents, "unicode_escape")
Note that this will change
foo\nbar\\nbash\u1234
to
foo
bar\nbashሴ
You will also want to handle errors. You can do this either by catching UnicodeDecodeError
or by using an error replacement policy:
contents = input()
contents = codecs.decode(contents, "unicode_escape", errors="replace")
Sadly this seems to mess with unicode characters:
codecs.decode("α", "unicode_escape")
#>>> 'α'
The simplest fix I know of is to first escape with raw_unicode_escape
:
contents = input()
contents = contents.encode("raw_unicode_escape")
contents = contents.decode("unicode_escape")
This is probably far more complicated than you need, so I suggest not actually doing this.
As Martijn explained you'll need to process the replacements yourself. The easiest way to do that is literally with the .replace
method:
>>> print(input('Enter \\n for newline: ').replace('\\n', '\n'))
Enter \n for newline: This is my \nnewline
This is my
newline
This will work fine for the \n
escape sequence, but if you want others (e.g. \t
) then you'll need to implement that yourself.
\n
is an escape sequence that only works in string literals. input()
does not take a string literal, it takes the text the user inputs and doesn't do any processing on it so anyone entering \
followed by n
produces a string of two characters, a backslash and the letter n
, not a newline.
You'll have to do your own processing of such escapes yourself:
file = file.replace(r'\n', '\n')
Here I used a raw string literal, which also doesn't support escapes sequences, to define the literal backslash \
followed by a n
.
Alternatively, repeatedly ask users for a new filename, until they are done:
lines = []
print('Type in your document, followed by a blank line:')
while True:
line = input("> ")
if not line:
break
lines.append(line)
file = '\n'.join(lines)
Demo:
>>> lines = []
>>> print('Type in your document, followed by a blank line:')
Type in your document, followed by a blank line:
>>> while True:
... line = input("> ")
... if not line:
... break
... lines.append(line)
...
> foo
> bar
>
>>> lines
['foo', 'bar']
>>> '\n'.join(lines)
'foo\nbar'