python - 尝试使用 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.*> 及其内容
解决方案
您只想删除标签,而不是标签之后的所有内容,因此无需添加额外的.*?
这是你的最终代码
re.sub('<s.*?>','',line)
推荐阅读
- matlab - 我试图找出“下标索引必须是真正的正整数或逻辑”错误
- django - 将模型字段值保存为另一个字段的默认值
- javascript - Javascript / 在胖箭头函数中丢失上下文
- html - trelliscopejs R 封装只有一个在 html 文件中可见的 trelliscope 图形
- pypdf2 - .extractText() 返回“无效的十进制文字”
- python - Python 子进程不会跟我说话
- java - 使用 @Transactional 时未从其余服务返回数据
- android-fragments - 旋转Android应用程序时片段中recyclerView中clickListener上的NPE
- postgresql - Google 地图是否支持打印带有从数据库中提取的纬度和经度值的标记的功能?
- python - 将函数应用于列后重建numpy数组