0

我有一个包含测试用例排序的 CSV 文件,我有一个无序的 xml 文件,我正在尝试根据 csv 文件对 xml 文件进行排序。这样做时,我收到此错误:-

TypeError                                 Traceback (most recent call last)
<ipython-input-208-7ef10cfb792b> in <module>
     28     for TestCaseName in csv_order:
     29         try:
---> 30             children.append(tests.pop(TestCaseName))
     31         except KeyError:
     32             print(f"Test '{TestCaseName}' not present")

TypeError: unhashable type: 'list'

这是我的代码:-

from bs4 import BeautifulSoup
import csv

# Read in the XML and parse it

with open(r'example_xml/DM12_new.xml') as f_input:
    soup = BeautifulSoup(f_input, 'xml')

# Store all test tags in a dictionary

tests = {}

for test in soup.find_all('Test'):
    tests[test['Name']] = test

# Locate the children tag and empty it

children = soup.find('Children')
children.clear()

# Read in the ordering CSV and append the tests in the required order into
# the children tag

with open(r'csv/SPT.csv', newline='') as f_order:
    csv_order = csv.reader(f_order)
    header = next(csv_order)

    for TestCaseName in csv_order:
        try:
            children.append(tests.pop(TestCaseName))
        except KeyError:
            print(f"Test '{TestCaseName}' not present")

# Add any remaining tests not listed in the CSV

for test in tests.values():
    children.append(test)

# Write the modified XML

with open(r'example_xml/output.xml', 'w') as f_output:
    f_output.write(str(soup))

这里有什么问题有人可以帮我解决吗?提前致谢

4

1 回答 1

0

这里

with open(r'csv/SPT.csv', newline='') as f_order:
    csv_order = csv.reader(f_order)
    header = next(csv_order)

    for TestCaseName in csv_order:
        try:
            children.append(tests.pop(TestCaseName))
        except KeyError:
            print(f"Test '{TestCaseName}' not present")

TestCaseName正在保存来自给定 csv 文件行的所有值,这是您的意图还是应该是其中一列的值,例如TestCaseName[0]第一列?

于 2021-07-08T08:45:26.907 回答