Need help filtering out incomplete entries in a .csv [Python]
Question: I have a set of data points, which look like this:

ID 70014 1940 1 1 26.8 1 Y

ID 70014 1940 1 2 29.8 1 Y

ID 70014 1940 1 3 34.3 1 Y

ID 70014 1940 1 4 35.7 1 Y

ID 70014 1940 1 5 34.1 1 Y

but some of the entries have missing values, like the ones below:

ID 70014 1940 6 30  
ID 70014 1940 7 1 14 1 N

I need to use the def function to set the parameters for legit entries and
then filter them out, but I'm not exactly sure how to go about that. I'm
getting confused about where it should be located in my code, I'm fairly
certain I know how to describe the parameters, but not how to connect this
code to the rest of my program.

If I've left anything out, let me know, I'm happy to describe the issue more
thoroughly :)

Cheers.


Answer: The simplest thing you could do is splitting the string and count if there are
enough fields:

    
    
    def is_valid_ID(Id):
       if len(Id.split()) == 8:
           return True
       else:
           return False
    
     >>>is_Valid_Id("ID 70014 1940 1 1 26.8 1 Y")
     True
     >>>is_Valid_Id("ID 70014 1940 1 1 26.8 1")
     False
    

However, this wont check if the individual elements are valid (You could use
regexps for that).

Or the same thing in more compact notation

    
    
    def is_valid_ID(Id):
        return (True if len(Id.split()) == 8 else False)
    

If you want to use regexps you could try

    
    
     import re
    
     def accept_entry(entry):
         try:
             return re.search("ID [0-9]{5} [0-9]{4} [0-9] [0-9] [0-9.]* [0-9] Y",entry).group()
         except:
             return False
    

