【问题标题】:Find and replace text in string [duplicate]查找和替换字符串中的文本[重复]
【发布时间】:2016-09-22 17:19:37
【问题描述】:

我怎样才能从

获得https://onion-rip.de

https://onion-rip.de/data/images/9d5faef5c26f69546f8/68c8a2a5fe0a9c5c221a2bb.jpg

并替换为https://test.de。输出应该是:

https://test.de/data/images/9d5faef5c26f69546f8/68c8a2a5fe0a9c5c221a2bb.jpg

在 Python 中有什么特殊的功能吗?

【问题讨论】:

  • 基本 Python 和互联网上都有示例。例如尝试str.replace(x, y)。几乎所有语言都有字符串替换功能。
  • 请不要提出通过互联网搜索或仅查看文档即可找到答案的问题。
  • 首先显示您尝试过的内容。你会从那里得到帮助......
  • 老实说,不要在这里问这种问题。谷歌搜索很容易找到答案。看看 Python 关于stringurlparse 的文档。

标签: python


【解决方案1】:

使用 Python 的 .replace(),如下例所示:

x = "Hello world!"
y = x.replace("Hello", "Bye")
print y #Will print out "Bye World!"

【讨论】:

    【解决方案2】:

    您可以使用 Python 的 replace() 方法来执行此操作:

    oldUrl = 'https://onion-rip.de/data/images/9d5faef5c26f69546f8/68c8a2a5fe0a9c5c221a2bb.jpg'
    newUrl = oldUrl.replace('https://onion-rip.de', 'https://test.de')
    print(newUrl)
    

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2013-04-05
      • 2016-01-04
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多