Python re.findall 与 groupdicts [英] Python re.findall with groupdicts

查看:47
本文介绍了Python re.findall 与 groupdicts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点希望有一个 re.findall 版本返回 groupdicts 而不仅仅是 groups.我是否缺少一些简单的方法来实现相同的结果?(有人知道这个功能不存在的原因吗?)

I kind of wish that there were a version of re.findall that returned groupdicts instead of just groups. Am I missing some simple way to accomplish the same result? (Does anybody know of a reason that this function doesn't exist?)

推荐答案

您可以使用 finditer() 函数.这将为您提供一系列匹配对象,因此您可以获得每个对象的 groupdict:

You could use the finditer() function. This will give you a sequence of match objects, so you can get the groupdict for each with:

[m.groupdict() for m in regex.finditer(search_string)]

这篇关于Python re.findall 与 groupdicts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆