0
  • If you run

    tmpl = "This is the first line\n And this is the second line"
    print("tmpl)
    

    you get

     This is the first line
     And this is the second line
    

    So you get a new line expanded.

  • But if you write in a file, you will not get that:

    Put in test.tmpl:

    This is the first line\n And this is the second line
    

    and run

    with open("test.tmpl") as f:
        contents = f.read()
        print(contents)
    

    you get

    This is the first line\n And this is the second line
    

Why this behaviour? How can you get the the contents displays the same than tmpl?

somenxavier
  • 1,206
  • 3
  • 20
  • 43
  • if you `print(repr(contents))` you will see that it is escaped `This is the first line\\n And this is the second line` because it's not new-line char – buran Jan 16 '23 at 09:28
  • The file contains a backslash followed by a letter n, not a newline character. A newline character in your file will appear as a new line. – khelwood Jan 16 '23 at 09:28
  • Your test.tmpl does not contain a newline character. Maybe you wrote the file using a raw string or otherwise escaped the backslash. Show how you created the file content – DarkKnight Jan 16 '23 at 09:28
  • @Pingu how can do that? – somenxavier Jan 16 '23 at 09:29
  • @buran How can I read unscaped, so? – somenxavier Jan 16 '23 at 09:30
  • @somenxavier, the question is how to write it there as new-line char. If you have `\n` in a plain text file that is NOT new-line char. – buran Jan 16 '23 at 09:30
  • 2
    Try *contents = f.read().replace(r'\n', '\n')* – DarkKnight Jan 16 '23 at 09:33
  • @Pingu: that will not work properly for escaped backslashes like in `r'\\n'` – Thomas Weller Jan 16 '23 at 09:35
  • @ThomasWeller True but that doesn't appear to be the case with the data shown by OP. Also note the word "try" as we cannot be absolutely certain what the data looks like. If was providing a definitive solution I would do so by way of an Answer rather than a Comment – DarkKnight Jan 16 '23 at 09:42

1 Answers1

1

A Python string is interpreted by the Python interpreter. The Python interpreter knows what escape characters are and how to deal with them.

When reading a text file, you get the characters as they are. A newline in a text file consists of the characters 0x0D (CR; carriage return) and/or 0x0A (LF; line feed). You get that when pressing Enter on your keyboard. If you want to consider escape characters in a text file, you need to implement that yourself.

Applied to your case:

with open("test.tmpl") as f:
    contents = f.read()
    contents = bytes(contents, "utf-8").decode("unicode_escape")
    print(contents)
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • There is no way to escape characters automatically when read a file? – somenxavier Jan 16 '23 at 09:30
  • @somenxavier: I've added a link. It's not much code. – Thomas Weller Jan 16 '23 at 09:34
  • At least it's anti-intuitive behaviour – somenxavier Jan 16 '23 at 09:38
  • @somenxavier: That's not unintuitive for anyone, except for programmers. Nobody else than programmers cares about escape characters. So I guess you as a developer should adapt to the other 99% of text file users, not the other way round. – Thomas Weller Jan 16 '23 at 09:40
  • @somenxavier, if you write/create that file content manually, why would you use escape sequence `\n`, instead of new line? And if it is created programmatically, that means there is problem with how you write it and should change that, not how you read it. – buran Jan 16 '23 at 10:01