S
S
swww2020-07-13 13:25:03
Python
swww, 2020-07-13 13:25:03

ValueError: I/O operation on closed file. How to handle an error?

Good day. There is data that is stored in a dictionary, for example
{ 'Name_1' : 'Helen' , 'Name_2' : 'Michael' , 'Age_1' : '31' , 'Age_2' : '25'} There are many such dictionaries, they are all stored in the list. But it happens that the dictionary does not have, for example, 'Age_1' and when writing through a loop to a CSV file, it gives an error KeyError: 'Age_1' and ValueError: I/O operation on closed file. When handling an error through except KeyError: pass From the place where the error occurs, empty lines are written. Help how to handle the error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Larisa .•º, 2020-07-13
@swww

so check it out

if 'Age_1' in custom__dict:
    #  потом уже, что-то делать 
     ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question