【发布时间】:2016-01-11 19:38:15
【问题描述】:
这可能是一个简单的解决方法,但是我很难理解它;我正在从不同的脚本中读取行,并想用变量替换一行,但是它用 box1 而不是值 55, 55.7 替换它
box1 = 55, 55.7
box2 = -9, -7
with open('parttest', 'r') as file :
filedata = file.read()
filedata = filedata.replace('box_lat', 'box1')
filedata = filedata.replace('box_lon', 'box2')
with open('buildtest', 'w') as file:
file.write(filedata)
【问题讨论】:
-
在 Python 中(单引号或双引号)内的任何内容都是字符串
-
@sam2090 或三重或六重引号!
"""these too""" '''and these!''' -
@AdamSmith 是的。多行字符串,以防将来的读者不知道他们叫什么