I am working with a .csv file with three columns of data separated by ','. I copy two of the three columns into two lists to process the information later. The copying is done successfully on all lines but when I get to the end of the file I have two empty rows that cause me to get an error for trying to assign what is on those lines (nothing) to three variables.
The code is the following:
# Abrimos el archivo.
csvarchivo = open(direct)
# Leemos todos los registros.
data = csv.reader(csvarchivo)
# Creamos las listas en las que almacenar los datos de cada columna.
col_02 = []
col_03 = []
# Guardamos las columnas segunda y tercera en una lista cada una.
for row in data:
# Leemos la fila row y guardamos cada elemento en una variable.
element_01, element_02, element_03 = row
# Asignamos cada variable a su lista.
col_02.append(float(element_02))
col_03.append(float(element_03))
# Cerramos el archivo.
data.close()
How can I make it ignore blank lines and thus avoid getting an error?
PS: Erasing the lines by hand is not an option.
When you read an empty line, the variable
row
will also be empty. You can check that fact and skip that line (continue
).On the other hand, the last line of the code was wrong, since the file to close is
csvarchivo
and notdata
.Note however that it can still give you errors if the row you read does not have exactly three columns. You can skip the problematic lines like so:
Update . At the request of the user, I explain a little the logic of:
For python some data types evaluated in a boolean expression context, have a "value of truth". In the case above,
row
it's actually a list, but since we're evaluating it in the context of a boolean expression (in aif
) Python will use its "truth value".The truth value of a list is
True
whether the list has elements, orFalse
if it has none.Therefore it
not row
will giveTrue
if the row in question is an empty list, which is what happens when the CSV had a blank line. In that case the body of the will be executedif
, which sayscontinue
.The operation
continue
means: "drop everything that remains to be done in this iteration of the loop and move on to the next iteration".The
else
is not necessary, since if the condition is not met (because itrow
has elements), the will simplycontinue
not be executed, and therefore we will continue executing what comes after theif
.Another way to do the same thing, but which I have preferred not to use because it increases the indentation one more level, would be:
In this case
if row
it is read as "if there is data in the row", and then it processes it, while if there is not, the body of theif
will not be executed and we will go to the next iteration of the loop.