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)
python startswith与endswith的更多相关文章
- python startswith和endswith
startswith判断文本是否以某个或某几个字符开始; endswith判断文本是否以某个或某几个字符结束; text = 'Happy National Day!' print text.star ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...
- python 中startswith()和endswith() 方法
startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- python中strip、startswith、endswith
strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: ...
- PYTHON startswith (endswith类似)
Python startswith()方法Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- Python startswith()方法
描述 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. 语法 ...
随机推荐
- FreeMarker缓存处理
FreeMarker 的缓存处理主要用于模版文件的缓存,一般来讲,模版文件改动不会很频繁,在一个流量非常大的网站中,如果频繁的读取模版文件对系统的负担还是很重的,因此 FreeMarker 通过将模版 ...
- 常用 GDB 命令中文速览
转自:https://linux.cn/article-8900-1.html?utm_source=index&utm_medium=moremore 目录 break -- 在指定的行或函 ...
- socket io 记得flush
public class Client { public static void main(String args[]) throws Exception { //为了简单起见,所有的异常都直接往外抛 ...
- nodejs 语法很特别的地方
1. 不过我们之前说过了有 this 和没 this 的时候作用域不同,那个参数只是作用于构造函数中,而加了 this 的那个则是成员变量.用一个 this 就马上区分开来他们了,所以即使同名也没关系 ...
- Spring中AOP的初窥和入门小案例
AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...
- 爬虫之MongoDB的图片
聚合:
- Hadoop- Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10
如题出现Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10, 2019-01-03 05:36:14,774 INFO ...
- print webpage
使用浏览器打印网页时(A4纸)有一个固定的尺寸: 高级浏览器: width:700px height:1000px
- Advanced SQL: Relational division in jOOQ
i Rate This Relational algebra has its treats. One of the most academic features is the ...
- R语言基础入门之二:数据导入和描述统计
by 写长城的诗 • October 30, 2011 • Comments Off This post was kindly contributed by 数据科学与R语言 - go there t ...