0

たとえば、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 48 Hours: 1cm / Primary:  / Base Depth: 179cm</description>

<title>Lake Louise</title>

<description>Open  / Past 48 Hours: 2cm / Primary:  / Base Depth: 162cm</description>

find()を使用して、興味のある場所を見つけ、次の行を読んでさらにプログラミングするにはどうすればよいですか?基本的に、特定の行を見つけて、見つけた行の下の次の行を読む方法は?

ありがとう。

4

3 に答える 3

1

あなたが使用することができます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')

出力:

Open  / Past 48 Hours: 2cm / Primary:  / Base Depth: 162cm

SAX XML 解析を調べることもできます

于 2013-02-04T05:12:16.893 に答える
1

itertools.dropwhile() を使用してみてください:

Python 2.7.3 (default, Sep  4 2012, 20:19:03) 
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> src="foo\nbar\nbas\n"
>>> notfoundyet=True
>>> def findthing(x):
...     global notfoundyet
...     currently=notfoundyet
...     notfoundyet=x!="bar"
...     return currently
... 
>>> itertools.dropwhile(findthing, src.split("\n"))
<itertools.dropwhile object at 0x8017d92d8>
>>> for x in _: 
...     print x
... 
bas
于 2013-02-04T05:00:35.963 に答える
0
def getLine(source, string):
    source = source.split('\n')
    for index, item in enumerate(source):
        if string in item:
            return source[index + 1]
于 2013-02-04T04:47:59.853 に答える