Pabloo LR
Pabloo LR

Reputation: 43

Python count numbers of null valors in my imported csv file

I've exported a raster (in ascii) file to csv with hundres of rows and cols. Null valors are strored with the value -999. I've created a script to count the numbers of -999 in every row and col in this csv file, but doesn't work fine because always gets 0, however there are several -999 in the csv file. This is my code:

def CountError (csv):
    file=open(csv,"r")
    count=0       
    for i in file:
        for x in i:
            if x =="-999":
                count +=1      
    return count

    file.close()
    csv="MDT25.csv"
    print CountError (csv)

Do you know what need my code to work properly? Python code samples to count this would be greatly appreciated!
But I'm new in Python I don't want to import modules like csv o collections to get this.

Thanks!

Upvotes: 0

Views: 183

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117876

You need to split each row into elements using the ',' character as a delimeter

for i in file:
    for x in i.split(','):    # split each row on , character
        if x =="-999":
            count +=1   

Upvotes: 1

Related Questions