import re
pattern = '\[.+\]'
replace = '[num1, num2, num3, num4]'
file = open('code.txt', 'w')
for line in file:
if 'ente<value type="vector">' in line:
re.sub(pattern, replace, line)
new_vals = [1, 2, 3, 4]
f1 = open('code.txt', 'r')
f2 = open('code_out.txt', 'a+')
for line in f1:
newline = line
if 'ente<value type="vector">' in line: # check line by line and look if the prefix matches
newline = 'ente<value type="vector">' + f'[{new_vals[0] {new_vals[1]} {new_vals[2]} {new_vals[3]}]'
# replace the new line
f2.write(newline)
f1.close()
f2.close()
使用正则表达式查找模式并替换值:
只需用新值替换
num1
、num2
、num3
、num4
如果你不想在任何数学中使用它们,就让它们成为字符串格式
你可以试试这样的
code.txt<;-带代码的文件
相关问题 更多 >
编程相关推荐