How do I keep txt file formatting when importing it into a multiline string in Python

592 Views Asked by At

So I'm making a rather large game in Ren'py which features a lot of letters from characters, and because I don't want to create an rpy file clogged with 5000 lines of letters, I'm storing them in plain text files instead.

However, I want to keep the formatting when I import them into the game, so I want to import them as multiline strings. How do I keep the formatting in the txt file when I import it into the game?

i.e. I want to keep every newline in the txt file (just formatted as plain old newlines, not \n or anything) when I import it as a multiline string.

1

There are 1 best solutions below

0
On

You can just read the entire file at once:

with open("file.txt", "r") as file:
    message = file.read()

This will preserve the original file contents.