python根据开头和结尾字符串获取中间字符串的方法

yipeiwu_com6年前Python基础

本文实例讲述了python根据开头和结尾字符串获取中间字符串的方法。分享给大家供大家参考。具体分析如下:

这里给定一个字符串,指定开头和结尾的字符串,返回中间包夹的字符串,比如:
content:<div class="a">jb51.net</div>
startStr:<div class="a">
endStr:</div>
返回结果:jb51.net

def GetMiddleStr(content,startStr,endStr):
  startIndex = content.index(startStr)
  if startIndex>=0:
    startIndex += len(startStr)
  endIndex = content.index(endStr)
  return content[startIndex:endIndex]
if __name__=='__main__':
  print(GetMiddleStr('<div class="a">jb51.net</div>','<div class="a">','</div>'))

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python使用pyshp库读取shapefile信息的方法

通过pyshp库,可以读写Shapefile文件,查询相关信息,github地址为 https://github.com/GeospatialPython/pyshp import...

Python base64编码解码实例

Python中进行Base64编码和解码要用base64模块,代码示例: #-*- coding: utf-8 -*- import base64 str = 'cnblogs'...

python对指定目录下文件进行批量重命名的方法

本文实例讲述了python对指定目录下文件进行批量重命名的方法。分享给大家供大家参考。具体如下: 这段python代码可对c:\temp目录下的所有文件名为”scroll_1”文件替换为...

Django实现跨域的2种方法

jsonp 方式一:指定返回方法 # 后端 def view(request): callback = request.GET.get('callback') return...

python数组循环处理方法

简介 本文主要介绍python数组循环语法。主要方式有元素遍历,索引遍历,enumerate, zip, list内部等。 普通循环 list1 = ['item1', 'item2...