0

I'm currently writing a python script that cleans and order the data of a csv file , the script works fine but one problem remains which is to verify if the cell is empty or contains just a space like " "

this the condition that i've tried but it didn't work

k is a list

if k[i][j] is "  " or k[i][j] is None  :

            print('this cell is empty ',k.index(k[i][j]))

i expected that the condition will return the index of the cell but it didn't I'm wodering how to fix this problem ? thank you in advance

1 Answers1

1

An improvement suggestion: for space checking a more robust solution could be the function str.isspace(), because this function also considers newlines, tabs and other unicode space characters. See this related question.

So your code could look like this (first checking for None and then calling a function):

if k[i][j] is None or k[i][j].isspace():
    ...

Then, to get the index, your code would suggest that i and j already are the coordinates of the cell. Am I understanding your code wrong?

You could just print those variables then:

print('this cell is empty ', i, j)

Does that solve your issue?

Ralf
  • 16,086
  • 4
  • 44
  • 68