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程序设计有所帮助。

相关文章

Series和DataFrame使用简单入门

Series和DataFrame使用简单入门

(1)、导入库 from pandas import Series,DataFrame import pandas import numpy (2)、Series简单创建与使用...

Python环境搭建之OpenCV的步骤方法

一、openCV介绍 Open Source Computer Vision Library.OpenCV于1999年由Intel建立,如今由Willow Garage提供支持。Open...

TF-IDF算法解析与Python实现方法详解

TF-IDF算法解析与Python实现方法详解

TF-IDF(term frequency–inverse document frequency)是一种用于信息检索(information retrieval)与文本挖掘(text m...

python分割和拼接字符串

关于string的split 和 join 方法对导入os模块进行os.path.splie()/os.path.join() 貌似是处理机制不一样,但是功能上一样。1.string.s...

Python实现处理逆波兰表达式示例

本文实例讲述了Python实现处理逆波兰表达式。分享给大家供大家参考,具体如下: 中文名: 逆波兰表达式 外文名: Reverse Polish Notation 别名: 后缀表达式 逆...