写python程序循环在json格式raw_data中提取xml格式字段为ptcid的值
时间: 2023-03-27 19:05:10 浏览: 114
你好,我可以回答这个问题。可以使用Python中的json库来解析json格式的数据,然后使用正则表达式或者BeautifulSoup库来提取xml格式字段为ptcid的值。具体实现可以参考以下代码:
import json
import re
from bs4 import BeautifulSoup
raw_data = '{"name": "John", "age": 30, "xml_data": "<root><ptcid>123456</ptcid><content>hello world</content></root>"}'
data = json.loads(raw_data)
xml_data = data['xml_data']
soup = BeautifulSoup(xml_data, 'xml')
ptcid = soup.find('ptcid').text
print(ptcid)
输出结果为:123456
希望能对你有所帮助。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)