简体   繁体   English

将txt文件导入Python中的多行字符串时如何保持txt文件格式

[英]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.所以我在 Ren'py 中制作了一个相当大的游戏,其中包含很多来自字符的字母,并且因为我不想创建一个被 5000 行字母阻塞的 rpy 文件,所以我将它们存储在纯文本文件中反而。

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?导入游戏时如何保留txt文件中的格式?

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.即,当我将它作为多行字符串导入时,我想保留 txt 文件中的每个换行符(只是格式化为普通的旧换行符,而不是 \n 或任何东西)。

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.这将保留原始文件内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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