3 回答

TA貢獻1773條經驗 獲得超3個贊
您需要打開文件進行讀?。J),然后需要打開文件進行寫入:
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將是文本文件中的整個字符串。
添加回答
舉報