Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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 替换字符串中的句点(";)_Python - Fatal编程技术网

Python 替换字符串中的句点(";)

Python 替换字符串中的句点(";),python,Python,我有一个类型为。/sometext/someothertext的字符串,我正在尝试将字符串中的。替换为网站的名称http://www.website.com 在Python中,我所做的是这样的: strName = "../sometext/someothertext" strName.replace("..", "http://www.website.com") print strName 但我得到的唯一结果是 ../sometext/someothertext 我也试着避开经期,比如 s

我有一个类型为
。/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
不会修改原始字符串,但会返回一个经过修改的新字符串。

如果有其他字符串,最好指定一个替换字符串。。在那里

strName = strName.replace("..", "http://www.website.com", 1)

我不是巨蟒。但是,如果../总是出现在开头,那么我将从2开始添加子字符串,并在开头添加网站文本。我想这会更快。事实上,这不是一个到位的方法。哦,糟糕。。。在阅读了Python文档之后,我认为这是一个就地替代。谢谢我现在觉得自己像个白痴P