0

I came upon this code in a tutorial:

 for row in csv_reader:
        if not row:
            continue
        dataset.append(row)

Which I take to mean that if the code encounters something other than a row, just skip and continue. Is that correct?

What defines 'not row'?

spacedustpi
  • 351
  • 5
  • 18
  • 1
    An empty line. Maybe you have a blank line and you want to ignore it. This would give you all non-empty lines. – idjaw Jul 23 '18 at 02:19
  • 1
    not negates whatever is passed to it. not True is False. – skrubber Jul 23 '18 at 02:19
  • 1
    It's checking to see of the "truthiness" of `row` is `False` which would indicate the value returned was an empty sequence of elements. – martineau Jul 23 '18 at 02:20
  • I thought it might be an empty row among other things, but I can't seem to find definitive literature. @ martineau, you sound pretty definitive so I'll go with that until something reveals that it is not. Thanks. – spacedustpi Jul 23 '18 at 02:24
  • 1
    Possible duplicate of [What is Truthy and Falsy in python? How is it different from True and False?](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-in-python-how-is-it-different-from-true-and-false) – Merik Jul 23 '18 at 02:26
  • @ Merik, thanks for the link. – spacedustpi Jul 23 '18 at 02:27

2 Answers2

1

This allows you to skip over empty lines in a CSV file.


not row just means "row is falsey".

In Python, the following things are falsey:

  • False
  • None
  • Numeric zeroes
  • Empty containers

(Of course you can write your own class with a __bool__ method that does anything you want—but by convention, it should follow the same rule.)

This is all explained in Boolean operations in the docs.

--

The rows iterated by a csv.reader are lists. (And lists are containers, so they're falsey iff they're empty.)

An empty line produces an empty list; a line with text but no delimiters produces a list of one string; a line with delimiters produces a list of two or more strings.

This is covered in the csv module docs… but not really all in one place.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

row is just a variable name in this context. When you do if row, you actually checking if there is any content to the variable that python considers to be True.

Take a look at this answer from Patrick Haugh in which he highlights lots of examples of what is Falsy in python.

To illustrate in a minimal example:

import csv
for row in csv.reader(['row1,foo','', 'row3,bar']):
    print(row)

yields

['row1', 'foo']
[]
['row3', 'bar']

But if you do

for row in csv.reader(['row1,foo','', 'row3,bar']):
    if row:
        print(row)

Then output is

['row1', 'foo']
['row3', 'bar']

and thus basically the empty row is filtered out.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • 1
    Surely you wanted to use the same list in your second example as in your first? Because that will just print out the line `['one', 'two', 'three', '']`. – abarnert Jul 23 '18 at 02:27
  • Perhaps it is more efficient to write: for row in csv_reader: if row: dataset.append(row) – spacedustpi Jul 23 '18 at 02:30