簡體   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