3 回答
TA贡献1773条经验 获得超3个赞
您需要打开文件进行读取(默认),然后需要打开文件进行写入:
def SettingFileParser(filepath):
with open(filepath, 'r') as read_file:
lines = read_file.readlines():
with open(filepath, 'w') as write_file:
for line in lines:
if line.startswith('PRIMER_PRODUCT_SIZE_RANGE='):
# keep the line the same except the part needing replacement
write_file.write(line.replace('100-300','500-1000'))
else:
# otherwise just write the line as is
write_file.write(line)
TA贡献1846条经验 获得超7个赞
具体来说,这样的事情可能会解决问题:
import re
# read in file here...
re.sub(r"PRIMER_PRODUCT_SIZE_RANGE=[0-9,-]*", "PRIMER_PRODUCT_SIZE_RANGE=500-1000", s)
在这里,s将是文本文件中的整个字符串。
添加回答
举报