用python分割路径

时间:2012-05-10 22:15:21

标签: python split urlparse

我试图在最后一个小数后删除所有内容并将“html”添加到结尾

HTML

<a href="http://www.youversion.com/bible/gen.1.ceb">http://www.youversion.com/bible/gen.1.ceb</a>

当前代码返回“gen.1.ceb”

name = urlparse.urlparse(url).path.split('/')[-1]

我想要名字来获得“gen.1.html”

2 个答案:

答案 0 :(得分:2)

你可以这样做:

filename = urlparse.urlparse(url).path.split('/')[-1]  # get file name
name = filename.rsplit('.', 1)[0] + '.html'  # change the extension

答案 1 :(得分:2)

import re
re.sub(r'\.[A-Za-z]+$','.html',url)