【发布时间】:2015-12-22 23:39:37
【问题描述】:
我正在尝试使用正则表达式从 xml 文件中获取一些数据(我知道我应该适当地使用一些 xml 解析器,但是如果您之前没有使用过任何 xml 解析器,正则表达式似乎更简单)
这是我的python代码:
import datetime,re
date= datetime.datetime.today().strftime('%Y%m%d')
output = open(r'C:\Users\Peter\AppData\Roaming\Kodi\addons\plugin.video.videoaddon\resources\epg.xml', 'r')
match = re.compile('start="%s(.+?)\s.+?".+?channel="Bravo (US)"\s><title>(.+?)</title>'%(date)).findall(output)
for start,title in match:
print(start,title)
我收到TypeError: Expected string or buffer
谁能解释我为什么会收到这个错误?
如果有人感兴趣,我正在尝试做的其他信息:
XML 文件示例:http://pastebin.com/9yC1FTYu 我知道这个 xml 格式不正确,但这就是我从软件 API 中获取它的方式。
基本上我需要得到:
start 时间戳的最后一部分,第一部分必须匹配日期,以及 <title> 文本。我需要在channel 匹配Bravo (US) 的地方得到这些。
【问题讨论】:
-
XML 没有格式错误,为什么不使用合适的 XML 解析器呢?
-
正如我所说,我从来没有使用过 xml 解析器的经验,我只是不知道该怎么做。正则表达式对我来说似乎更简单。
标签: python regex xml xml-parsing