• python startswith与endswith


    如果你要用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)

  • 相关阅读:
    delphi安装pngimage控件,不需要安装,只需引用就行
    怎样把PL/SQLDeveloper字体调大
    resource is out of sync with the file
    SecureCRT:[1]SecureCRT配色方案
    安装开发环境注意事项2
    插入排序
    tomcat更改端口序
    tomcat更改端口
    maven添加jar包
    java总结
  • 原文地址:https://www.cnblogs.com/liuchunxiao83/p/8434202.html
Copyright © 2020-2023  润新知