程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了TypeError: unhashable type: 'list' Python error while append大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决TypeError: unhashable type: 'list' Python error while append?

开发过程中遇到TypeError: unhashable type: 'list' Python error while append的问题如何解决?下面主要结合日常开发的经验,给出你关于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] 用于第一列?

大佬总结

以上是大佬教程为你收集整理的TypeError: unhashable type: 'list' Python error while append全部内容,希望文章能够帮你解决TypeError: unhashable type: 'list' Python error while append所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。