import re pattern1 = re.compile('<link rel="stylesheet" href="(.*?)">') #pattern2 = re.compile('<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>') pattern2 = re.compile('<script src="(.*?)"></script>') with open("./aaa.txt",'r') as f: ret = f.read() links = pattern1.findall(ret) res1 = '' link1 = """<link rel="stylesheet" href="{%static '""" link2 = """'%}"> """ for s in links: res1 += link1+s+link2 print(res1) # ================================= scripts = pattern2.findall(ret) res2 = '' script1 = """<script src="{%static '""" script2 = """'%}"></script> """ for s in scripts: res2 += script1+s+script2 print(res2)