在Python中使用正则表达式来删除XML中的空白行?

2024-10-03 02:36:58 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉,如果以前有人问过这个问题,但我在任何地方都找不到答案。。在

我试图使用regex来提取元素值,但是被提取的xml包含一个空白行,这似乎导致了错误。在

以下是XML中的一个元素:

<entry>
    <id>http://feeds.rasset.ie/rteavgen/player/videos/show/?id=10103822</id>
    <showid>10103822</showid>
    <platform>iptv</platform>
    <published>2013-01-19T21:45:00+00:00</published>
    <updated>2013-01-19T23:41:00+00:00</updated>
    <title type="text">The Saturday Night Show</title>
    <content type="text">Chat show, presented by journalist and broadcaster Brendan O'Connor, featuring comedy, celebrity guests and live musical performances.</content>
    <category term="RTÉ One" rte:type="channel"/>
    <category term="Entertainment" rte:type="genre"/>
    <category term="None" rte:type="series"/>
    <category term="None" rte:type="episode"/>
    <category term="None" rte:type="ranking"/>
    <category term="1024" rte:type="genrelist"/>
    <category term="None" rte:type="keywordlist"/>
    <category term="1668" rte:type="progid"/>
    <link rel="self" type="application/atom+xml" href="http://feeds.rasset.ie/rteavgen/player/playlist?showId=10103822"/>

    <link rel="alternate" type="text/html" href="http://www.rte.ie/player/#v=10103822"/>
    <rte:valid start="2013-01-19T21:52:12+00:00" end="2013-02-09T21:52:12+00:00"/>
    <rte:duration ms="4201061" formatted="1:10"/>
    <rte:statistics views="194"/>
    <media:title type="plain">The Saturday Night Show</media:title>
    <media:description type="plain">Chat show, presented by journalist and broadcaster Brendan O'Connor, featuring comedy, celebrity guests and live musical performances.</media:description>
    <media:player url="http://feeds.rasset.ie/rteavgen/player/player/?id=" width="400" height="300"/>
    <media:thumbnail url="http://img.rasset.ie/0006e56a.jpg" time="00:00:00+00:00"/>
    <media:restriction relationship="allow" type="country"/>
    <media:restriction relationship="disallow" type="country"/>
    <media:copyright>RTÉ</media:copyright>
</entry>

您可以看到两个“link rel=”元素之间有一个空行。在

当我尝试使用这个regex命令时,它会抛出超时!错误,因为我没有正确处理这个问题(请原谅,我的正则表达式知识几乎为零)。在

^{pr2}$

我实际上只需要一些字段,但我似乎找不到一个regex命令,它允许我只选择我想要的单个元素名称,它让我按顺序输入每个元素名(同样,我缺乏正则表达式知识也是问题所在)。除了我需要的第二个“link rel=”元素之外,我还需要一些字段,但是由于它一直落在这个元素上,所以我暂时将它们排除在外。在

谁知道ReGEX命令需要跳过空白行,也许还需要整理表达式,只提取我需要的元素?在

谢谢你们的帮助,希望。。。在


Tags: andnoneidhttp元素titletypelink
2条回答

要删除空行,不需要regex:

with open("my_file.xml") as xmlfile:
    lines = [line for line in xmlfile if line.strip() is not ""]

with open("my_file.xml", "w") as xmlfile:
    xmlfile.writelines(lines)

同样要解析xml文件,您可以简单地使用expat:http://docs.python.org/2/library/pyexpat.html或者甚至可能使用mini-dom:http://docs.python.org/2/library/xml.dom.minidom.html另一个非常好的方法是ElementTree:http://docs.python.org/2/library/xml.etree.elementtree.html

然而,regex并不推荐这样做,实际上这是个坏主意。在

您不应该像其他人所说的那样在这个任务中使用regex。在

回答你的实际问题:你对元素之间的空白太过具体了。在这种情况下,额外的空白会给你带来麻烦。很容易就没有空格:

<category term="None" rte:type="ranking"/><category term="1024" rte:type="genrelist"/>

补救方法:不要使用\n后跟8个空格,而是使用\s*(零个或多个空格字符)。在

相关问题 更多 >