【发布时间】:2021-11-30 19:54:22
【问题描述】:
我必须替换文件中特定行的字符串,并且当它被替换时,字符串的开头和结尾不应该有引号。
我必须从 file1 中读取内容并将其替换到 file2 中的特定行。
file1.txt:
File1Content - "Replace this line in file"
file2.tf:
var head{
default = File2Content - "This is file2."
}
var tail{
default = "$$Replace Here$$"
}
Python:
with open('file1.txt') as fd:
file1_text = fd.read()
with open('file2.txt') as fw:
file2_text = fw.read()
with open('file3.tf' , 'w') as fz:
fz.write(file2_text.replace("$$Replace Here$$",rep_string))
预期输出:
var head{
default = File2Content - "This is file2."
}
var tail{
default = File1Content - "Replace this line in file"
}
但我得到的是。
输出:
var head{
default = File2Content - "This is file2."
}
var tail{
default = "File1Content - "Replace this line in file""
}
我需要尾块作为 default = File1Content - "Replace this line in file" 我不希望在尾块中字符串的开头和结尾加上引号。有什么想法可以删除吗?
【问题讨论】:
标签: python string replace quotes