Python 3.x 删除两个其他字符串之间的字符串
我有一个字符串,我需要删除两个其他字符串之间的字符串中的字符 目前我有以下代码,我不太清楚为什么它不工作Python 3.x 删除两个其他字符串之间的字符串,python-3.x,string,re,Python 3.x,String,Re,我有一个字符串,我需要删除两个其他字符串之间的字符串中的字符 目前我有以下代码,我不太清楚为什么它不工作 def removeYoutube(itemDescription): itemDescription = re.sub('<iframe>.*</iframe>','',desc,flags=re.DOTALL) return itemDescription def removeYoutube(itemDescription): itemDescr
def removeYoutube(itemDescription):
itemDescription = re.sub('<iframe>.*</iframe>','',desc,flags=re.DOTALL)
return itemDescription
def removeYoutube(itemDescription):
itemDescription=re.sub('.'','',desc,flags=re.DOTALL)
退货项目描述
它不会删除介于和之间的字符串
输入示例(字符串):
”
预期输出:
从输出中可以看到,它应该删除包含
的所有部分使用BeautifulSoup
而不是regex
,因为regex
对于解析HTML
来说是一个糟糕的选择
以下是方法:
from bs4 import BeautifulSoup
sample = """
<div style="text-align: center;"><iframe allowfullscreen="frameborder=0" height="350" src="https://www.youtube.com/embed/EKaUJExxmEA" width="650"></iframe></div>
"""
s = BeautifulSoup(sample, "html.parser")
for tag in s.find_all(True):
if tag.name == "iframe":
tag.extract()
print(s)
从bs4导入美化组
样本=”“
"""
s=BeautifulSoup(示例,“html.parser”)
对于s.find_all中的标记(True):
如果tag.name==“iframe”:
tag.extract()
印刷品
输出:
<div style="text-align: center;"></div>
通常,如果您提供示例输入和预期输出,您会得到更好的答案,因为这样可以减少歧义。输入中没有模式。只谢谢你的回答,我不知道为什么我没有想到这一点,谢谢你链接到这个页面来解释原因。将来会比使用正则表达式更多地使用它。非常感谢:)