【问题标题】:Remove <word> and </word> from a text using python [duplicate]使用 python 从文本中删除 <word> 和 </word> [重复]
【发布时间】:2021-03-18 13:19:22
【问题描述】:

我有问题。我正在尝试使用 python 从句子中删除&lt;script&gt;&lt;/script&gt;。但我只能删除脚本这个词。我也不能去掉尖括号。

句子是:sentence = "&lt;script&gt; document.write("Hello word");&lt;/script&gt;"

我想要的输出:document.write("Hello word");

我在 python 解释中找不到类似的东西。

这可能吗,那我需要使用什么?

【问题讨论】:

    标签: python


    【解决方案1】:

    你可以试试.replace

    sentence = sentence.replace("</script>","")
    sentence = sentence.replace("<script>","")
    #output sentence = " document.write('Hello word');"
    

    另外,你应该这样初始化sentence

    sentence = "<script> document.write('Hello word');</script>"
    

    如果你也在字符串中使用",你会得到一个SyntaxError: invalid syntax 错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2015-08-12
      相关资源
      最近更新 更多