python - 有没有办法从另一个字符串中获取一个特定的字符串?
问题描述
假设我有下一个字符串:
string = "<dialog id="105">\xa<state>confirmed</state>\xa</dialog>"
此字符串的长度可能会有所不同。有没有一种方法可以让我只confirmed
在 python 中获取字符串?
解决方案
我稍微修改了您的 xml 字符串,并能够确认为输出,以下是代码:
import xml.etree.ElementTree as ET
string = '''<dialog id="105"><state>confirmed</state></dialog>'''
root = ET.fromstring(string)
print(root)
for child in root:
if child.tag == 'state':
print(child.text)
让我知道这个是否奏效。
推荐阅读
- javascript - 尝试在 react-google-maps 上卸载 4k+ 标记时冻结浏览器
- docker - Docker run not working 它说至少需要 1 个参数
- java - 在两个不同端口上具有两个服务的 Spring Boot 应用程序
- javascript - 在 Vue 组件中使用 Flow ESLint 和“flowtype/no-weak-types”
- ckeditor - TYPO3:CKEditor去除 -Tags中的数据属性
- security - 亚马逊 MWS(非 AWS)数据保护政策 - 网络控制
- xml - 使用 VBA 解析 XML SOAP 文档
- c# - 我在 Windows 窗体应用程序中遇到以下错误?任何人都可以帮助解决它
- mongodb - MongoDB InsertOne 中的点表示法
- python - 如何使用方括号传递字符串变量?