我有一个带有字符串的Python脚本,我想用另一个脚本替换字符串。除了相关字符串之外,我不允许修改原始脚本中的任何其他内容。
script.py
string1 = 'OriginalValue'
replaceString.py
string1 = 'newValue'
script.py
string1 = 'newValue'
这是可能的,如果是的话,怎么样?
答案 0 :(得分:1)
以下replaceString.py应该这样做:
with open('script.py') as myFile:
content = myFile.readlines()
with open('script.py', 'w') as myFile:
for line in content:
newLine = line.replace("string1 = 'OriginalValue'", "string1 = 'newValue'")
myFile.write(newLine)