英文:
Slice and extract specific values from JSON output
问题
我正在查询一个REST API,我需要从适配器输出中选择2个字段,查询将包括多个事件编号。
我基本上需要一个事件编号和描述的列表,从下面的输出中获取。
获取数据的代码:
response = requests.post('http://dev.startschools.local:2031/baocdp/rest/process/:ITSM_Interface:IncidentManagement:QueryIncident/execute', headers=headers, data=json.dumps(get_query_inc_json()))
print(response.text)```
响应文本打印输出:
```[{"name":"Adapter_Output","value":"<query-action-result><entries><metadata><entry-count>2</entry-count></metadata><entry id=\"INC000003197686|INC000003197686\"><field name=\"Incident Number\">INC000021</field><field name=\"Description\">Student needs a new card issued</field></entry><entry id=\"INC000003198967|INC000003198967\"><field name=\"Incident Number\">INC000035</field><field name=\"Description\">Students cannot access the text book portal</field></entry></entries></query-action-result>"}]```
我需要获取一个事件编号和描述的列表,这样我就可以循环遍历它们:
事件编号列表1:
```["INC000021", "INC000035"]```
描述列表2:
```["Student needs a new card issued", "Students cannot access the text book portal"]```
我正在尝试使用切片将所有事件编号放入列表中,但由于输出不断变化,切片无法正常工作。
有人知道除了切片之外,还有更高效的方法来从这个输出中提取我需要的信息吗?
<details>
<summary>英文:</summary>
I am querying a REST API and I need to select 2 fields from the adapter output below, the query will include multiple incident numbers.
I basically need a list of Incident Numbers and Descriptions from the output below.
Code to get the data:
headers = {'content-type': 'application/json', 'Authentication-Token': authToken}
response = requests.post('http://dev.startschools.local:2031/baocdp/rest/process/:ITSM_Interface:IncidentManagement:QueryIncident/execute', headers=headers, data=json.dumps(get_query_inc_json()))
print(response.text)
response.text printed Output:
[{"name":"Adapter_Output","value":"<query-action-result><entries><metadata><entry-count>2</entry-count></metadata><entry id="INC000003197686|INC000003197686"><field name="Incident Number">INC000021</field><field name="Description">Student needs a new card issued</field></entry><entry id="INC000003198967|INC000003198967"><field name="Incident Number">INC000035</field><field name="Description">Students cannot access the text book portal</field></entry></entries></query-action-result>"}]
I need to get a list of Incident numbers and descriptions so I can loop through them:
List 1 of incident numbers:
["INC000021", "INC000035"]
List 2 of Descriptions:
["Student needs a new card issued", "Students cannot access the text book portal"]
I am trying to use slice to get all the incident numbers into a list but because the output keeps changing the slice is not working.
Anyone know of a more efficient way other than slice to extract the info I need from this output?
</details>
# 答案1
**得分**: 1
尝试这个。
data = response.text
value = data[0]["value"]
i_nums = [x.split("</field")[0] for x in value.split('<field name="Incident Number">')[1:]]
desc = [x.split("</field")[0] for x in value.split('<field name="Description">')[1:]]
print(i_nums)
print(desc)
<details>
<summary>英文:</summary>
Try this.
data = response.text
value = data[0]["value"]
i_nums = [x.split("</field")[0] for x in value.split('<field name=\"Incident Number\">')[1:]]
desc = [x.split("</field")[0] for x in value.split('<field name=\"Description\">')[1:]]
print(i_nums)
print(desc)
</details>
# 答案2
**得分**: 1
即使 @jjislam 的答案给出了你例子的正确答案,我仍建议你使用适当的 XML 解析器来浏览你的结果,因为这实际上是首次使用结构化数据的意图!
这是如何使用内置的 `xml.etree` 包的片段。一旦你理解这里发生了什么,你可以很容易地将其转换为使用推导实现的单行代码。
导入 xml.etree.ElementTree
响应文本 = [{"name":"Adapter_Output","value":"<query-action-result><entries><metadata><entry-count>2</entry-count></metadata><entry id=\"INC000003197686|INC000003197686\"><field name=\"Incident Number\">INC000021</field><field name=\"Description\">Student needs a new card issued</field></entry><entry id=\"INC000003198967|INC000003198967\"><field name=\"Incident Number\">INC000035</field><field name=\"Description\">Students cannot access the text book portal</field></entry></entries></query-action-result>"}]
# 根将是最顶层元素,在本例中是带有标签 "query-action-result" 的元素
root = xml.etree.ElementTree.fromstring(responseText[0]["value"])
事件,描述 = []
# 递归地遍历根元素的所有标签为 "entry" 的元素,这些元素是根元素的子元素
对于条目中的条目:
# 在每个条目中,找到第一个标签为 "field" 且属性为 "name=Incident Number" 的元素,并捕获其内容
事件 = 条目.find("field[@name='Incident Number']").text
# 同样,但属性为 "name=Description"
描述 = 条目.find("field[@name='Description']").text
打印(事件,描述)
事件.append(事件)
描述.append(描述)
打印(事件)
打印(描述)
<details>
<summary>英文:</summary>
Even though @jjislam's answer gives you the right answer for your example, I would recommend you to make use of a proper xml parser to navigate through your result, because this is the actual intent of having structured data in the first place!
This is a snippet on how to use the built-in `xml.etree` package. Once you get what is going on here, you can easily convert it into a single-liner with comprehensions.
import xml.etree.ElementTree
responseText = [{"name":"Adapter_Output","value":"<query-action-result><entries><metadata><entry-count>2</entry-count></metadata><entry id=\"INC000003197686|INC000003197686\"><field name=\"Incident Number\">INC000021</field><field name=\"Description\">Student needs a new card issued</field></entry><entry id=\"INC000003198967|INC000003198967\"><field name=\"Incident Number\">INC000035</field><field name=\"Description\">Students cannot access the text book portal</field></entry></entries></query-action-result>"}]
# the root will be the topmost element, in this case the one with tag "query-action-result"
root = xml.etree.ElementTree.fromstring(responseText[0]["value"])
incidents, descriptions = [], []
# iterate recursively over all elements with tag "entry" that are children of the root element
for entry in root.iter('entry'):
# within each entry, find the first element with tag "field" and attribute "name=Incident Number", and capture its content
incident = entry.find("field[@name='Incident Number']").text
# same, but with attribute "name=Description"
description = entry.find("field[@name='Description']").text
print(incident, description)
incidents.append(incident)
descriptions.append(description)
print(incidents)
print(descriptions)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论