[英]How to remove quotes from start and end of the string at a specific line in python?
我必须在特定行替换文件中的字符串,并且在替换时字符串的开头和结尾不应该有引号。
我必须从 file1 中读取内容并将其替换为 file2 中的特定行。
文件 1.txt:
File1Content - "Replace this line in file"
文件 2.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))
预计 Output:
var head{
default = File2Content - "This is file2."
}
var tail{
default = File1Content - "Replace this line in file"
}
但我得到的是。
Output:
var head{
default = File2Content - "This is file2."
}
var tail{
default = "File1Content - "Replace this line in file""
}
我需要尾块作为默认值 = File1Content - “替换文件中的这一行”我不希望尾块中字符串的开头和结尾有引号。 任何想法删除它?
尝试替换此行:
with open('file3.tf' , 'w') as fz:
ind.write(file2_text.replace("$$Replace Here$$",rep_string))
有了这个:
with open('file3.tf' , 'w') as fz:
ind.write(file2_text.replace('"$$Replace Here$$"',rep_string))
模板文件已包含引号:
var head{
default = File2Content - "This is file2."
}
var tail{
default = "$$Replace Here$$"
}
如果您不想要它们,请删除它们:
var head{
default = File2Content - "This is file2."
}
var tail{
default = $$Replace Here$$
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.