0

I have a simple loop which gets stuck on a division by zero error. I am running a bool to filter out zero-value denominators, but for some reason the bool I used isn't working. Can someone please help? I am using python 2.6.5

Here is a sample from my code:

for i in range(0,len(lines)):
    line = lines[i]
    line = line.split(";")
    leansz = line[9]
    FW = line[34]
    FS = line[35]  
    print "FS: %s  %s"%(FS,type(FS))  #troubleshooting the denominator and its type
    if FS == "0":  #I have tried FS == 0 and FS == "0" to no avail
        print 'FS == "0"' #checking if bool is working
        continue  #return to top of loop if denominator is zero
    LnSzRa = float(leansz)/(float(FS)/2)  #division by zero error   

Here is a sample of what is returned and then the error:

FS: 184
  <type 'str'>
FS: 1241
  <type 'str'>
FS: 2763
  <type 'str'>
FS: 1073
  <type 'str'>
FS: 971
  <type 'str'>
FS: 0
  <type 'str'>
Traceback (most recent call last):
  File "mpreader.py", line 50, in <module>
    LnSzRa = float(leansz)/(float(FS)/2)
ZeroDivisionError: float division
teachamantofish
  • 75
  • 1
  • 1
  • 7

1 Answers1

2

Your FS value is a string that includes the newline character from the file still, so test for a string value:

if FS == '0\n':

or strip the newline:

    if FS.strip() == '0':

or turn FS into a float first:

if float(FS) == 0:

or strip line while splitting:

line = line.strip().split(';')

Further tips:

  • Just loop over lines directly; don't use range():

    for line in lines:
        line = line.strip()
        # do something with `line`
    

    Even if you still need an index as well, you use enumerate() to generate the index:

    for i, line in enumerate(lines):
        line = line.strip()
        # do something with `line` and `i`.
    
  • You can use the csv module to handle splitting data files into rows:

    import csv
    
    with open(somefile, 'rb') as inputfile:
        reader = csv.reader(inputfile, delimiter=';')
        for row in reader:
            leansz, FW, FS = map(float, (row[9], row[34], row[35]))
            if not FS: continue
            LnSzRa = leansz / (FS / 2)
    
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Actually, I just edited this. I have tried FS == "0" originally. It returned the same error. I just tried with FS == "0" again, but no success. – teachamantofish Aug 07 '13 at 20:59
  • @user2136314: ah, you also have the newline still in the value. – Martijn Pieters Aug 07 '13 at 21:02
  • part of the issue with not using the CSV module is my lack of knowledge of the "with open as file" syntax, which is used in all the documentation I could find about the CSV module. I learned python 2.4 originally and now I am now using python 2.6.5. Does 2.6.5 support that syntax? – teachamantofish Aug 07 '13 at 21:06
  • @user2136314: Context managers were added in Python 2.5 (with a `from __future__ import with_statement` syntax), and are fully supported in Python 2.6. – Martijn Pieters Aug 07 '13 at 21:07
  • @user2136314: The CSV module doesn't require using files as context managers however, a simple `open()` then explicit `.close()` later on would suffice. – Martijn Pieters Aug 07 '13 at 21:08
  • I used the strip line method and it worked. Thank you for all your tips. The additional comments beyond original intent of my question were very helpful and appreciated! – teachamantofish Aug 07 '13 at 21:23