正在尝试搜索 EPG XML-数据

Trying to search EPG XML-data

我正在尝试搜索 XML 格式 (xmltv) 的 EPG(电子节目指南)。我想找到所有包含特定文本的节目,例如今天哪些频道将播放特定的足球(足球)比赛。 样本数据(真实数据>20000个元素):

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
<tv generator-info-name="TX" generator-info-url="http://epg.net:8000/">
<channel id="GaliTV.es">
    <display-name>GaliTV</display-name>
    <icon src="http://logo.com/logos/GaliTV.png"/>
</channel>
<programme start="20210814080000 +0200" stop="20210814085500 +0200" channel="GaliciaTV.es" >
        <title>A Catedral de Santiago e o Mestre Mateo</title>
        <desc>Serie de catedral de Santiago de Compostela.</desc>
    </programme>
    <programme start="20210815050000 +0200" stop="20210815055500 +0200" channel="GaliciaTV.es" >
        <title>santiago</title>
        <desc>Chili.</desc>
    </programme>
</tv>

我只想在 titledesc 属性包含特定文本(不区分大小写)时显示 <programme> 属性。使用 ElementTree,我试过这个:

for title in root.findall("./programme/title"):
   match = re.search(r'Santiago',title.text)
   if match:
       print(title.text)

它会找到一个结果,但是:

  1. 我收到一个我不明白的错误:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
  1. 我不知道如何搜索不区分大小写,[Ss]antiago不起作用。
  2. 我想 return 父元素的结果(例如 programme.attributes)。

你不会为此阅读正则表达式;尝试

for title in doc.findall('.//programme//title'):
    if "santiago" in title.text.lower():
        print(title.text)

样本的输出应该是

A Catedral de Santiago e o Mestre Mateo
santiago

编辑:

要从每个 programme 中获取所有数据,试试这个:

for prog in doc.findall('.//programme'):
    title = prog.find('title').text
    if "santiago" in title.lower():      
        start,stop,channel = prog.attrib.values()
        desc = prog.find('.//desc').text
        print(start,stop,channel,'\n',title,'\n',desc)
        print('-----------')

输出:

20210814080000 +0200 20210814085500 +0200 GaliciaTV.es 
 A Catedral de Santiago e o Mestre Mateo 
 Chili.
-----------
20210815050000 +0200 20210815055500 +0200 GaliciaTV.es 
 santiago 
 Chili.

我还要补充一点,如果 xml 变得稍微复杂一点,从 ElementTree 切换到 lxml 可能是个好主意,因为后者有更好的 xpath 支持。