在Python中找到一行并阅读下一行
例如,在Python中,我解码一个url并找到如下报告:在Python中找到一行并阅读下一行,python,line,Python,Line,例如,在Python中,我解码一个url并找到如下报告: <title>Canada Olympic Park</title> <description>Open / Past 48 Hours: 0cm / Primary: / Base Depth: 85cm</description> <title>Castle Mountain</title> <description>Open / Past
<title>Canada Olympic Park</title>
<description>Open / Past 48 Hours: 0cm / Primary: / Base Depth: 85cm</description>
<title>Castle Mountain</title>
<description>Open / Past 48 Hours: 1cm / Primary: / Base Depth: 179cm</description>
<title>Lake Louise</title>
<description>Open / Past 48 Hours: 2cm / Primary: / Base Depth: 162cm</description>
加拿大奥林匹克公园
打开/过去48小时:0厘米/主要:/Base深度:85厘米
城堡山
开放/过去48小时:1厘米/主要:/Base深度:179厘米
路易丝湖
开放/过去48小时:2cm/主要:/Base深度:162cm
如何使用find()找到我感兴趣的地方并阅读下一行以进行进一步编程?
基本上如何找到一个特定的行,并阅读下一行我刚刚找到的行
谢谢。尝试使用itertools.dropwhile()
def getLine(source, string):
source = source.split('\n')
for index, item in enumerate(source):
if string in item:
return source[index + 1]
Python 2.7.3(默认,2012年9月4日,20:19:03)
[GCC 4.2.1 20070831在freebsd9上修补[FreeBSD]]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口itertools
>>>src=“foo\nbar\nbas\n”
>>>notfoundyet=True
>>>def查找内容(x):
... 全球尚未发现
... 当前=尚未找到
... notfoundyet=x=“酒吧”
... 当前返回
...
>>>itertools.dropwhile(findthing,src.split(“\n”))
>>>对于x英寸:
... 打印x
...
制动辅助系统
您可以使用xml.dom.minidom
假设xml数据位于文件中
from xml.dom.minidom import parse
xmldata = open("abc.txt", "r")
domdata = parse(xmldata)
def getDescriptionData(title):
titledata = [x.toxml().lstrip('<title>').rstrip('</title>') for x in domdata.getElementsByTagName('title')]
descriptiondata = [x.toxml().lstrip('<description>').rstrip('</description>') for x in domdata.getElementsByTagName('description')]
l = [v for (x, v) in zip(titledata, descriptiondata) if x == title]
if l:
return l[0]
return None
print getDescriptionData('Lake Louis')
你也可以调查一下
from xml.dom.minidom import parse
xmldata = open("abc.txt", "r")
domdata = parse(xmldata)
def getDescriptionData(title):
titledata = [x.toxml().lstrip('<title>').rstrip('</title>') for x in domdata.getElementsByTagName('title')]
descriptiondata = [x.toxml().lstrip('<description>').rstrip('</description>') for x in domdata.getElementsByTagName('description')]
l = [v for (x, v) in zip(titledata, descriptiondata) if x == title]
if l:
return l[0]
return None
print getDescriptionData('Lake Louis')
Open / Past 48 Hours: 2cm / Primary: / Base Depth: 162cm