Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从xml代码中提取数据并将其转换为python中的数组_Python_Arrays_Xml - Fatal编程技术网

从xml代码中提取数据并将其转换为python中的数组

从xml代码中提取数据并将其转换为python中的数组,python,arrays,xml,Python,Arrays,Xml,在用python向Web服务器发送请求后,我收到了XML代码,并对其应用了一些更改 import re s='<?xml version="1.0" encoding="utf-8"?><string xmlns="http://emts.erpguru.in/">{"data":[{"Id":0,"IsSuccess":true,"Msg&

在用python向Web服务器发送请求后,我收到了XML代码,并对其应用了一些更改

import re

s='<?xml version="1.0" encoding="utf-8"?><string xmlns="http://emts.erpguru.in/">{"data":[{"Id":0,"IsSuccess":true,"Msg":"MobileNo Already Exists"}] }</string>'

result = re.search('"data":\[{(.*?)}', s)
j= (result.group(1)).split(',')
print(j[2])
重新导入
s='{“数据”:[{“Id”:0,“issucess”:true,“Msg”:“MobileNo已存在”}]}
result=re.search('数据“:\[{(.*)}',s)
j=(result.group(1)).split(','))
打印(j[2])
输出:
“Msg”:“MobileNo已存在”

我需要一种更有效的方法将XML结果转换为数组,以便
print(j[“Msg”])
会有结果吗
MobileNo已经存在

Python中有一个XML模块:将其与JSON库结合,因为您的字符串包含嵌套在XML中的JSON:

导入json
从xml.dom.minidom导入解析字符串
s='{“数据”:[{“Id”:0,“issucess”:true,“Msg”:“MobileNo已存在”}]}
dom=解析字符串
json_string=dom.firstChild.firstChild.nodeValue
j=json.loads(json_字符串)
打印(j[“数据”][0]。获取(“消息”))

另一种方法。此库使用正则表达式实现

import json
from simplified_scrapy import SimplifiedDoc
html = '<?xml version="1.0" encoding="utf-8"?><string xmlns="http://emts.erpguru.in/">{"data":[{"Id":0,"IsSuccess":true,"Msg":"MobileNo Already Exists"}] }</string>'

doc = SimplifiedDoc(html)
s = doc.string.html
# Or
s = doc.select('string').html
result = json.loads(s)
print (result["data"][0]["Msg"])

这里有更多的例子:

Python中有一个XML模块:将其与JSON库(
import JSON
JSON.loads(一些字符串)
)结合起来,因为您的字符串包含嵌套在XML中的JSON。
MobileNo Already Exists