Skip to content Skip to sidebar Skip to footer

How To Export Selected Dictionary Data Into A File In Python?

Right now when I check this function, in export_incomes if income_types in expenses: TypeError: unhashable type: 'list' I am not sure what I did wrong here. I did create an incomes

Solution 1:

First, you start your question with expenses but ends with incomes and the code also has incomes in the parameters so I'll go with the income.

Second, the error says the answer. "expense_types(income_types)" is a list and "expenses(incomes)" is a dictionary. You're trying to find a list (not hashable) in a dictionary.

So to make your code work:

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)

If I made any wrong assumption or got your intention wrong, pls let me know.

Post a Comment for "How To Export Selected Dictionary Data Into A File In Python?"