确保没有页面部分的URL列表始终以斜杠和点结尾

时间:2011-10-25 20:31:26

标签: python

我有一个非常大的网址列表,我想在没有网页部分的所有网址上添加斜杠和点(/.)。以下是我想要实现的一些例子:

  • http://www.example.com应为http://www.example.com/.
  • http://www.example.com/index.htm没问题
  • http://www.example.com/.没问题
  • http://www.example.com/#应为http://www.example.com/.
  • http://www.example.com/something应为http://www.example.com/something/.

一个例外是哈希(#)也应该用点替换。

我可以使用正则表达式来执行此操作,因为这是我唯一能够突然出现的内容,但由于这是非常大的10000+ URL列表,我正在寻找最快的方法。

由于

1 个答案:

答案 0 :(得分:0)

通过“页面部分”,你似乎意味着最后带有'.htm'的东西(给出你的第2和第5个例子),所以:

url = url.rstrip('/#.')
if not url.endswith('.htm'):
    url += '/.'