0

Is there some python way to convert a text (from a file for example) into a format string?
I mean for a text file:

this is a {format}
string.

Load it in python and have it become like the triple quotes format string:

var = """this is a {format}
string."""

I know how to just read the file and replace the curly braces, but I was wondering if there is already something that does this.

Thanks

Edit: This is the code I've tried:

with open('file.txt', 'r') as rs:
    lines = rs.readlines()
text = ','.join(lines)
print(text)
text.format(format='something_else')
print(text)

It just prints the text file. I'm looking to know if there is a more pythonic way then me having to write a class that does this. Thanks

wjk2a1
  • 776
  • 1
  • 7
  • 16

1 Answers1

0

variable.format() returns the formatted text and does not change the content of variable. Try

print(text.format(format="bla"))

or

text = text.format(format="bla")
Nils Werner
  • 34,832
  • 7
  • 76
  • 98