4
我正在嘗試RegEx字符串輸出並將其添加到字典中作爲文本組,並且我正在使用re.split()
。正則表達式將文本字符串拆分爲字典作爲組
我的字符串輸出爲:
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
這裏我解析的代碼:
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
你如何得到很好地格式化當你的字符串沒有任何新行時輸出? – nhahtdh