1

I have multiple items within my xml code I want to parse. I'm not quite sure of how to do it, any help would be greatly appreciated. Below is a snippet of my xml and python code and what I'm looking to do.

XML

<doc>
    <para>
        <text> /PARSEME: ABC12345/         /PARSEME: ABC98765/         /PARSEME: FGB87654/
        </text>
    </parse>
</doc>

Python code

def get_parseme(self, document):
    match = self.getNodeContent(document.contents(), 'para', 'text', true)
    match2 = re.search(r"PARSEME:\D{3}\d{5}", match, re.M|re.I)
    if match2:
        return match2.group()

2 Answers 2

0

re.search() - Scan through a string, looking for any location where this RE matches.

findall() - Find all substrings where the RE matches, and returns them as a list.

from http://docs.python.org/2/howto/regex.html

Here is working example

#!/usr/bin/env python
import re
match = ' /PARSEME: ABC12345/         /PARSEME: ABC98765/         /PARSEME: FGB87654/'
match_parse = re.findall(r"PARSEME: (\D{3}\d{5})", match, re.M|re.I)
if match_parse:
    print match2
0

Have you looked at the ElementTree XML API? http://docs.python.org/2/library/xml.etree.elementtree.html

I've found it to be very helpful when trying to parse XML files. Try http://effbot.org/zone/element-index.htm for some extra basic documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.