简体   繁体   中英

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

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?

ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM