首页 > 解决方案 > 尝试使用 Python 替换 html 标记时擦除以下标记

问题描述

我有以下格式的word doc

test1.docx

 ["<h58>This is article|", "", ", "<s1>Author is|", "<s33>Research is on|", "<h4>CASE IS|", "<s6>1-3|"]

试图找到以 <s.*?> 开头的标签并将标签及其内容替换为 ""

def locatestag():
 fileis = open("test1.docx")

 for line in fileis:
   print(line)
   newfile = re.sub('<s.*?> .*? ','',line)

with open("new file.json","w") as newoutput:
   son.dump(newfile, newoutput)

最终的输出文件也会使标签消失。

最终的内容就像

["<h58>This is article|", "", ", ]

如何仅在保留标签的其余部分(即保留标签)的同时删除 <s.*> 及其内容

标签: pythonre

解决方案


您只想删除标签,而不是标签之后的所有内容,因此无需添加额外的.*?

这是你的最终代码

re.sub('<s.*?>','',line)

推荐阅读