如何从字符串中提取子字符串? [英] How do I extract substring from string?

查看:138
本文介绍了如何从字符串中提取子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个xml字符串,我需要从中识别这些值并从中省略名称。我想的逻辑是提取

>之间的所有字符串。和< /。 

然后删除任何提取的子字符串,其中包含>,<或< /在其中





XML字符串是:



< ServiceInstanceUpdateAck xmlns ='http://www.loremipsum.com/'> 
< Context>
< Attribute name ='Atri1'> A1< / Attribute>
< Attribute name ='Atri2'> A2< / Attribute>
< Attribute name ='Atri3'> A3< / Attribute>
< / Context>
< Operation> DEPROVISION< / Operation>
<状态>成功< /状态>
<错误消息='无错误'majorErrorCode ='E_NONE'minorErrorCode ='E_NONE'/>
< Service>
< Id> 11233334< / Id>
< / Service>
< / ServiceInstanceUpdateAck>







这将是什么python代码?我正在学习python而且还没有那么好的字符串。感谢任何帮助。

解决方案

您不应该以这种方式处理XML,使用适当的XML解析器,例如 20.6。 xml.dom - 文档对象模型API& mdash; Python 3.3.6文档 [ ^ ]。

I have an xml string here from which I need to identify the values and leave out names from it. The logic I thought is to extract all strings which are in between of

> and </.

Then remove any extracted substring which has  >,< or </ in it



XML string is:

<ServiceInstanceUpdateAck xmlns='http://www.loremipsum.com/'>
  <Context>
    <Attribute name='Atri1'>A1</Attribute>
    <Attribute name='Atri2'>A2</Attribute>
  <Attribute name='Atri3'>A3</Attribute>
  </Context>
  <Operation>DEPROVISION</Operation>
  <Status>SUCCESS</Status>
  <Error message='No Error' majorErrorCode='E_NONE' minorErrorCode='E_NONE'/> 
  <Service>
    <Id>11233334</Id>
  </Service> 
</ServiceInstanceUpdateAck>




What will be the python code for that? I am learning python and not that good with strings yet. Any help is appreciated.

解决方案

You should not treat XML in this way, use a proper XML parser such as 20.6. xml.dom — The Document Object Model API &mdash; Python 3.3.6 documentation[^].


这篇关于如何从字符串中提取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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