Issue
List elements to be appended in XML data:
Sorted_TestSpecID: [10860972, 10860972, 10860972, 10860972, 10860972]
Sorted_TestCaseID: [16961435, 16961462, 16961739, 16961741, 16961745]
Sorted_TestText : ['SIG1', 'SIG2', 'SIG3', 'SIG4', 'SIG5']
original xml data:
<tc>
<title>SIG1</title>
<tcid>2c758925-dc3d-4b1d-a5e2-e0ca54c52a47</tcid>
<attributes>
<attr>
<key>TestSpec ID</key>
<value>1</value>
</attr>
<attr>
<key>TestCase ID</key>
<value>2</value>
</attr>
</attributes>
</tc>
Trying Python script to:
- Search title SIG1 in xml data from Sorted_TestText
- Then it should search for Key =TestCase ID and update the corresponding 16961435 value
- Then it shall check for its resp. Key =TestSpec ID and update the corresponding 10860972.
I am able to append TestCase ID with below code but all value attributes are getting updated with Sorted_TestCaseID list. Intention is that Values shall get updated in XML based on key attributes(Sorted_TestSpecID & Sorted_TestCaseID). Stuck here in differentiating two value attributes based on key.
Any sort of help shall be appreciated.
soup = BeautifulSoup(xml_data, 'xml')
all_items = soup.find_all('tc')
for set in all_items:
title = set.find('title').text
print (title)
if title in Sorted_TestText:
for p, new_value in zip(soup.select('value'), Sorted_TestCaseID):
p.string = str(new_value)
Solution
Consider adding an attributes
level loop after zipping through list elements. Then, conditionally check keys for value assignment:
soup = BeautifulSoup(xml_data, 'xml')
for tc in soup.find_all('tc'):
for title, spec, case in zip(Sorted_TestText, Sorted_TestSpecID, Sorted_TestCaseID):
if tc.find('title').text == title:
for attr in tc.find_all('attr'):
if attr.find('key').text == "TestSpec ID":
attr.find('value').text = str(spec)
if attr.find('key').text == "TestCase ID"
attr.find('value').text = str(case)
print(soup)
Answered By - Parfait
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.