Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 删除两个其他字符串之间的字符串_Python 3.x_String_Re - Fatal编程技术网

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>


通常,如果您提供示例输入和预期输出,您会得到更好的答案,因为这样可以减少歧义。输入中没有模式。只
谢谢你的回答,我不知道为什么我没有想到这一点,谢谢你链接到这个页面来解释原因。将来会比使用正则表达式更多地使用它。非常感谢:)