11

I have a CSV file that I'm reading in Python and I want the program to skip over the row if the first column is empty. How do I do this?

Right now I have:

with open('testdata1.csv', 'rU') as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            if row[0] = null:
                #?????

How do I: 1) Check for empty cells in the CSV; and 2) tell the reader to skip the row?

Thanks guys.

korakorakora
  • 197
  • 1
  • 2
  • 11

6 Answers6

21
with open('testdata1.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        print(row)
        if not row[0]:
             print("12")

Reference: How do I detect missing fields in a CSV file in a Pythonic way?

jamylak
  • 128,818
  • 30
  • 231
  • 230
Kit Fung
  • 481
  • 3
  • 10
  • Great! What about skipping the row (as in not reading it, and moving to the next)? – korakorakora Dec 10 '15 at 02:37
  • 1
    if row[0] in (None, ""): continue – Kit Fung Dec 10 '15 at 02:42
  • 3
    `if row[0] in (None, "")` can be more Pythonically written as `if not row[0]` – jamylak Dec 10 '15 at 03:05
  • @jamylak `if not row[0]` will not mean the same as `if row[0] in (None, "")` in case of 0 as the value in the cell of a CSV. – YDF May 06 '21 at 13:39
  • @YDF That would be `"0"` not `0` – jamylak May 07 '21 at 01:43
  • @jamylak not exactly. I mean here we are checking for an empty cell in the CSV. Whereas a `0` as an integer value in the condition `if not row[0]` will result in true indicating that the cell is empty which is actually not the case. – YDF May 07 '21 at 04:51
  • @YDF But coming from `csv.reader` it would always have string values so that would never happen, unless I'm mistaken, that's why that case would not happen – jamylak May 10 '21 at 03:56
3

You could use try and except.

for row in csvreader:
        try:
               #your code for cells which aren't empty
        except:
               #your code for empty cells
Palak Bansal
  • 810
  • 12
  • 26
1

I tried out what happens if you print and empty row and it returns [] (empty brackets)... so just check for that.

with open('testdata1.csv', 'rU') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        if row == []:
            continue
        #do stuff normally
   
0

After hour of trying I was able to do it

while act_tab.cell(row=i, column=1).value is not None:
    ...
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Small adaptation of Kit Fung's answer:

with open('testdata1.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
    if not row[0]:
         continue  # this will skip to the next for loop iteration
    # do your processing here
Apollo Data
  • 1,267
  • 11
  • 20
  • `if not row[0]` will not account the same for `0` as the value in the cell of a CSV then for something like `None or ""`. – YDF May 06 '21 at 14:01
0

I would assume if you len() it then would be zero i.e.

if not len(row[0])
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77