考慮這個:正則表達式來提取標記及其內容
input = """Yesterday<person>Peter</person>drove to<location>New York</location>"""
一個如何使用正則表達式模式來提取:
person: Peter
location: New York
這個效果很好,但我不想硬編碼的標籤,他們可以改變:
print re.findall("<person>(.*?)</person>", input)
print re.findall("<location>(.*?)</location>", input)
你越來越接近危險的http://stackoverflow.com/a/1732454/3001761 – jonrsharpe
@DevEx請參閱修改的答案 – PyNEwbie