如果你要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith
比如:content = 'ilovepython'
如果字符串content以ilove开始,返回True,否则返回False
content.startswith("ilove")
返回true
content.startswith("sss")
返回false

如果字符串content以python结尾,返回True,否则返回False
content.endswith('python')
返回true
content.endswith("sss")
返回false

下面这个例子是我写了个文件替换的小程序。替换所有.html文件里的图片的路径

import os
import re
t = re.compile(r'\/?static\/|\/?media\/') #re.compile

template = '/home/laowangpython/'
for root, dirs, files in os.walk(template):
    for f in files:
        if f.endswith('.html'):
            tihuan = 'http://www.cnpythoner.com/'
            filename = '%s'%(os.path.join(root,f))
            print filename
            f_a = file(filename,'r')
            info = []
            for i in f_a:
                content =  t.sub(tihuan,i)
                info.append(content)
            finfo = "".join(info)
            b = file(filename,'w')
            b.write(finfo)

python startswith与endswith的更多相关文章

  1. python startswith和endswith

    startswith判断文本是否以某个或某几个字符开始; endswith判断文本是否以某个或某几个字符结束; text = 'Happy National Day!' print text.star ...

  2. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  3. Python: 字符串开头或结尾匹配str.startswith(),str.endswith()

    问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...

  4. python 中startswith()和endswith() 方法

    startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...

  5. Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...

  6. python中strip、startswith、endswith

    strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: ...

  7. PYTHON startswith (endswith类似)

    Python startswith()方法Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end ...

  8. 在Javascript中使用String.startsWith和endsWith

    在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...

  9. Python startswith()方法

    描述 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. 语法 ...

随机推荐

  1. MySql基础学习-数据操作

    对于数据的操作,无非是“增删该查”这几种操作. 1增加 数据的增加使用insert语句插入 语法:INSERT INTO TABLE_NAME(column1,column2,...)VALUES(v ...

  2. node express框架基本配置

    node express框架基本配置 初始化项目 express -e 安装依赖包 npm install 安装第三方包 npm install xxx --save-dev dos 运行node a ...

  3. 开启 cmd cmder 代理

    win10安装了ShadowSocks软件,浏览器通过代理后就可以***,但有时候需要通过cmd科学下载安装一些组件,就需要设置一下cmd的代理 cmd如果要设置代理的话,需要在执行其他命令之前,先执 ...

  4. NAVICAT PREMIUM 初识

    1 问题运行SQL取数语句出错?. 答案:因为运行SQL文件时没有这个表.新的数据库里面 解决方法: 获取需要建立的二维表创建语句 新建查询 输入语句,点击运行即可 解释注释语句: 主体为五个字段的主 ...

  5. nginx安装目录

    1.rpm -ql nginx看看通过yum安装到哪里了 2./etc/logrotate.d/nginx    配置 nginx日志轮转 用于logrotate服务的日志切割 3./etc/ngin ...

  6. win7与vbox虚拟机Ubuntu设置共享文件夹

    1.进入Ubuntu系统,在右上角打开设备->安装增强功能->运行->输入密码 2.在终端中安装,安装完后重新启动Ubuntu 3.在本机中设置一个共享文件夹(文件名是vbox-sh ...

  7. JavaWeb学习总结(二) Servlet

    本文目录 一.Servlet概述 二.Servlet接口 三.GenericServlet 四.HttpServlet 五.Servlet细节 六.ServletContext 回到顶部 一.Serv ...

  8. LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. Spring与RMI集成实现远程访问

    使用spring对RMI的支持,可以非常容易地构建你的分布式应用.在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter ...

  10. Microsoft Visual Studio 2012 Update 4 RC 3 离线安装程序

    Microsoft Visual Studio 2012 Update 4 RC 3 离线安装程序 ☆ 微软官网地址:☆ http://www.microsoft.com/en-us/download ...