繁体   English   中英

TypeError: unhashable type: 'list' Python error while append

[英]TypeError: unhashable type: 'list' Python error while append

我有一个包含测试用例排序的 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))

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

这里

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM