python – RegEx将文本字符串拆分为dict作为组

python – RegEx将文本字符串拆分为dict作为组,第1张

概述我正在尝试RegEx字符串输出并作为一组文本添加到字典中,我正在使用re.split(). 我的字符串输出是: mpathag (36005076801b2014804000000000001cd) dm-7 Test ,2145size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw|-+- policy='service-tim 我正在尝试RegEx字符串输出并作为一组文本添加到字典中,我正在使用re.split().

我的字符串输出是:

mpathag (36005076801b2014804000000000001cd) dm-7 Test,2145size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw|-+- policy='service-time 0' prio=50 status=active| |- 12:0:1:3 sdas  66:192 active ready  running| `- 13:0:1:3 sdbi  67:192 active ready  running`-+- policy='service-time 0' prio=10 status=enabled  |- 12:0:0:3 sdak  66:64  active ready  running  `- 13:0:0:3 sdba  67:64  active ready  runningmpathz (36005076801b2014804000000000001c4) dm-0 Test,2145size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw|-+- policy='service-time 0' prio=50 status=active| |- 12:0:0:0 sdah  66:16  active ready  running| `- 13:0:0:0 sdax  67:16  active ready  running `-+- policy='service-time 0' prio=10 status=enabled   |- 10:0:0:0 sdb   8:16   active ready  running   `- 13:0:1:0 sdbf  67:144 active ready  running

这是我的解析代码:

output = "mpathag (36005076801b2014804000000000001cd) dm-7 Test,2145\    size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw\    |-+- policy='service-time 0' prio=50 status=active\    | |- 12:0:1:3 sdas  66:192 active ready  running\    | `- 13:0:1:3 sdbi  67:192 active ready  running\    `-+- policy='service-time 0' prio=10 status=enabled\      |- 12:0:0:3 sdak  66:64  active ready  running\      `- 13:0:0:3 sdba  67:64  active ready  running\    mpathz (36005076801b2014804000000000001c4) dm-0 Test,2145\    size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw\    |-+- policy='service-time 0' prio=50 status=active\    | |- 12:0:0:0 sdah  66:16  active ready  running\    | `- 13:0:0:0 sdax  67:16  active ready  running\     `-+- policy='service-time 0' prio=10 status=enabled\       |- 10:0:0:0 sdb   8:16   active ready  running\       `- 13:0:1:0 sdbf  67:144 active ready  running"    devices = re.split('mpath',output)    for dev in devices:       print dev

我的问题是re.split从文本中删除模式,它可能拆分而不删除分隔符?

输出结果如下:

ag (36005076801b2014804000000000001cd) dm-7 Test,2145    size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw    |-+- policy='service-time 0' prio=50 status=active    | |- 12:0:1:3 sdas  66:192 active ready  running    | `- 13:0:1:3 sdbi  67:192 active ready  running    `-+- policy='service-time 0' prio=10 status=enabled        |- 12:0:0:3 sdak  66:64  active ready  running        `- 13:0:0:3 sdba  67:64  active ready  running
解决方法 如果模式包含捕获组,则不会排除该组:

>>> re.split(',','1,2,3')  # without capturing group['1','2','3']>>> re.split('(,)',3')  # with capturing group['1','3']>>> xs = re.split('(,3')>>> [part1+part2 for part1,part2 in zip(xs[1::2],xs[2::2])][',2',3']
devices = re.split('(mpath)',output)for part1,part2 in zip(devices[1::2],devices[2::2]):    print part1 + part2
总结

以上是内存溢出为你收集整理的python – RegEx将文本字符串拆分为dict作为组全部内容,希望文章能够帮你解决python – RegEx将文本字符串拆分为dict作为组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1196020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存