有什么python方法可以将文本(例如从文件)转换为格式字符串吗?
我是说对于文本文件:
this is a {format}
string.
在python中加载它,使其成为三重引号格式的字符串:
var = """this is a {format}
string."""
我知道如何读取文件并替换花括号,但我想知道是否已经有这样的东西。你知道吗
谢谢
编辑: 这是我尝试过的代码:
with open('file.txt', 'r') as rs:
lines = rs.readlines()
text = ','.join(lines)
print(text)
text.format(format='something_else')
print(text)
它只是打印文本文件。 我想知道是否有一种比我必须写一个类来实现这一点更为python的方法。 谢谢
variable.format()
返回格式化文本,不更改variable
的内容。试试看或者
相关问题 更多 >
编程相关推荐