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 指定值,则在指定范围内检查. 语法 ...
随机推荐
- Hadoop的Docker镜像构建
1.Dockerfile ###Dockerfile -- beagin FROM ubuntu:trusty #MAINTAINER The Hue Team "https://githu ...
- 剑指Offer——圆圈中最后剩下的数(约瑟夫环)
Question 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后, ...
- org.apache.flume.ChannelException: Take list for MemoryTransaction, capacity 100 full, consider committing more frequently, increasing capacity, or increasing thread count
flume在抽取MySQL数据到kafka时报错,如下 [SinkRunner-PollingRunner-DefaultSinkProcessor] ERROR org.apache.flume.s ...
- Oralce查询后修改数据,弹窗报提示these query result are not updateable,include the ROWID to get updateable
select t.*, (select a.ANNEXNAME from base_annex a where a.id = t.closeFile) closeFileName, (select a ...
- python 处理json
Python处理JSON 概念 序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON.XML等.反序列化就是从存储区域(JSON,XM ...
- codeforces 705B:Spider Man
Description Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a ...
- shitf+tab
在eclipse中,shitf+tab可以使代码向左移动.
- bt种子文件是什么(包括bt文件结构)
bt种子文件是什么(包括bt文件结构) 一.总结 一句话总结:带特定格式特定信息(资源的url相关信息)的一个字符串(和json有点异曲同工之妙的感觉). 1.bt种子文件和json的区别和联系? 共 ...
- EF-局部更新
// ////1 public Task ReservedQuantity(long productId, long skuId, int reservedQuantity, long userId) ...
- Linux 性能监控分析
好文,参考 http://blog.csdn.net/hexieshangwang/article/details/47187185