zedzero
zedzero

Reputation: 31

In Python; How do you skip an invalid value in a 'for loop'?

I've got a for loop set up to read in an X and Y values from a .csv, so far so good.

However, the .csv is 65,000 rows long! and about 2000 of these are just blank rows.

How do I tell the for loop to ignore / skip blank rows and just get the next set of values?

I've been try to use 'continue' like this...

for line in lines:
    #split the content of each line to an array delimited by a comma ,
    pos = [x.strip() for x in line.split(',')]
    if pos == "":
        continue
    else:            
        #set up variables
        x = float(pos[0])*10000
        y = float(pos[1])*10000
        z = 0.0

But it doesn't work, whenever it gets to the first blank row it just adds zeros for the rest and I get this error message...

ValueError: empty string for float()

I did try removing the blank rows in Apple Numbers before Python imports them, but apparently something as simple as removing blank rows is a total ball-ache in Numbers.

I also tried to strip out all the blank variables before the For Loop, but couldn't get that to work either.

Any pointers would be greatly appreciated, but please keep it simple! (I'm new to this stuff and the more esoteric code goes right over my head).

Upvotes: 0

Views: 10440

Answers (3)

jfs
jfs

Reputation: 414255

From the exception: ValueError: empty string for float() I infer that "blank rows" mean something like '1, ,' in your case i.e., you get an error if any of the (first two) fields in a row is blank (contain only whitespace):

for line in lines:
    try:
        x, y = map(float, line.split(',')[:2])
    except ValueError:
        continue # skip invalid (for whatever reason) lines
    else:
        # use x, y here
        x *= 10000
        y *= 10000
        z = 0.0
        print(x, y, z)

Example

1,2
# the next line is blank AND IT IS SUCCESSFULLY SKIPPED NO 0,0

# missing second field
70,
# both empty
,
# first empty
,0
# 
,,80,90
3,4

Output

(10000.0, 20000.0, 0.0)
(30000.0, 40000.0, 0.0)

Upvotes: 0

skeept
skeept

Reputation: 12413

In the equality check pos is no longer a string it is a list. So you can check if the line itself is empty:

for line in lines:
    if line.strip() == "":
        continue
    #split the content of each line to an array delimited by a comma ,
    pos = [x.strip() for x in line.split(',') if x.strip()]
    # make sure you have x and y
    if len(pos) < 2:
        continue
    #set up variables
    x = float(pos[0])*10000
    y = float(pos[1])*10000
    z = 0.0

By adding the if condition while defining pos, we now discard empty elements.

Upvotes: 3

galaxyan
galaxyan

Reputation: 6121

I am newbie for python , but I think you could try the code below. My thought is If there is a empty line, the length of line should be 0. Thank you

 if not line:
       continue

Upvotes: 0

Related Questions