python - 替换字符串中的句点 (".")

标签 python

我有一个类型为 ../sometext/someothertext 的字符串,我正在尝试替换 ..在带有网站名称的字符串中 http://www.website.com .

在 Python 中,我所做的是这样的:

strName = "../sometext/someothertext"
strName.replace("..", "http://www.website.com")
print strName

但我得到的唯一输出是
../sometext/someothertext

我也试过逃避句号,比如
strName = ../sometext/someothertext
strName.replace("\.\.", "http://www.website.com")

但输出不会改变。我该怎么做呢?

最佳答案

你没有分配结果......

strName = strName.replace("..", "http://www.website.com")
.replace不修改原始字符串,而是返回一个带有修改的新字符串。

关于python - 替换字符串中的句点 ("."),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14750942/

相关文章:

python - 没有名为 flaskext.mysql 的模块

python - 使用 pandas 为每个组创建一个组合 DataFrame

python - 通过套接字发送多维 numpy 数组

python - Pandas 通过事件创建月末持有量

python base64转十六进制

python - 使用 serializers.ModelSerializer 进行过滤

python - 从元组列表创建嵌套字典的快速方法,无需 for 循环

java - 将音乐复制到 iPod 的 API

python - 从现有数据文件创建表,仅保留最后一行值

python - 从多重集中均匀采样