尝试write
到具有变量的文件,但是返回错误:
template = """1st header line
second header line
There are people in rooms
and the %s
"""
with open('test','w') as myfile:
myfile.write(template.format()) % (variable)
python大神给出的解决方案
.format
method希望您使用{}
样式而不是%s
来为要在字符串中填充空格的模板。它还期望将内插值作为其参数。
template = """1st header line
second header line
There are people in rooms
and the {}
"""
myfile.write(template.format(variable))