Python Regex“对象没有属性”
发布时间:2020-12-16 23:33:38 所属栏目:Python 来源:网络整理
导读:我一直在整理我们需要用新内容更新的页面列表(我们正在切换媒体格式).在这个过程中,我正在编辑正确拥有新内容的页面. 这是我正在做的一般想法: 迭代文件结构并获取文件列表 对于读取缓冲区的每个文件,使用正则表达式搜索匹配特定标记 如果匹配,则再测试2个
我一直在整理我们需要用新内容更新的页面列表(我们正在切换媒体格式).在这个过程中,我正在编辑正确拥有新内容的页面.
这是我正在做的一般想法: >迭代文件结构并获取文件列表 一切正常,直到第3个正则表达式模式匹配,我得到以下内容: ‘NoneType’对象没有属性’group’ # only interested in embeded content pattern = "(<embed .*?</embed>)" # matches content pointing to our old root pattern2 = 'data="(http://.*?/media/.*?")' # matches content pointing to our new root pattern3 = 'data="(http://.*?/content/.*?")' matches = re.findall(pattern,filebuffer) for match in matches: if len(match) > 0: urla = re.search(pattern2,match) if urla.group(1) is not None: print filename,urla.group(1) urlb = re.search(pattern3,match) if urlb.group(1) is not None: print filename,urlb.group(1) 谢谢. 解决方法
您的异常意味着urla的值为None.由于urla的值由re.search调用确定,因此re.search返回None.当字符串与模式不匹配时会发生这种情况.
所以基本上你应该使用: urla = re.search(pattern2,match) if urla is not None: print filename,urla.group(1) 而不是你现在拥有的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |