我正在以String格式检索错误消息。我必须切出信息。我已经尝试将其转换为列表,但这使整个消息进入单独的列表。尝试python substring /切片但无法检索消息。
错误消息如下所示
(vmodl.fault.InvalidArgument) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
msg = 'A specified parameter was not correct. \nspec.name',
faultCause = <unset>,
faultMessage = (vmodl.LocalizableMessage) [
(vmodl.LocalizableMessage) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'com.vmware.vim.vpxd.dvs.uniqueField1.label',
arg = (vmodl.KeyAnyValue) [
(vmodl.KeyAnyValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'dvs1',
value = 'ExampleDVS1'
},
(vmodl.KeyAnyValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'prop',
value = 'spec.name'
},
(vmodl.KeyAnyValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'value',
value = 'ExampleDVS1'
},
(vmodl.KeyAnyValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'op',
value = 'create'
},
(vmodl.KeyAnyValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 'dvs2',
value = 'ExampleDVS1'
}
],
message = 'A VDS ExampleDVS1 with spec.name as ExampleDVS1 already exists, cannot create VDS ExampleDVS1'
}
],
invalidProperty = u'spec.name'
}
如您所见,我想要检索一个消息字段为'A VDS ExampleDVS1 with spec.name as ExampleDVS1 already exists, cannot create VDS ExampleDVS1'
答案 0 :(得分:4)
试试这个:
re.search(r"message\s+=\s+'([^']*)'",text_string,re.M).group(1)