如何在Python中将选定的字典数据导出到文件中?

现在,当我检查此函数时,在export_venues中,如果在费用中的invenue_types:TypeError:unhashable type:'list',我不确定我在这里做错了什么。


我确实创建了一个收入字典,以 Income_types 作为键,以 Income_value 作为值。


迭代给定的收入字典,根据给定的收入类型(字符串列表)进行过滤,然后导出到文件。


将给定收入类型从给定收入字典导出到给定文件。


def export_incomes(incomes, income_types, file):

    final_list = []


    for u, p in expenses.items():

        if expense_types in expenses:

            final_list.append(':'.join([u,p]) + '\n')


    fout = open(file, 'w')

    fout.writelines(final_list)

    fout.close()

如果这是应在 txt 中显示的收入列表,如果用户输入股票、遗产、工作和投资,则每个项目和值应位于单独的行上:


库存:10000


房产:2000


工作:80000


投资:30000


茅侃侃
浏览 102回答 1
1回答

繁花如伊

首先,您的问题以支出开始,但以收入结束,代码的参数中也有收入,所以我将选择收入。其次,错误说明了答案。“expense_types(invenue_types)”是一个列表,“expenses(invenues)”是一个字典。您正在尝试在字典中查找列表(不可散列)。因此,要使您的代码正常工作:def export_incomes(incomes, income_types, file):    items_to_export = []    for u, p in incomes.items():        if u in income_types:            items_to_export.append(': '.join([u,p]) + '\n')  # whitespace after ':' for spacing        with open(file, 'w') as f:        f.writelines(items_to_export)如果我做出了任何错误的假设或误解了您的意图,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python