Reading csv file to python ValueError: could not convert string to float
Question: Any help with this would be appreciated. Please keep in mind that I am a
beginner in Python. This is the portion of the code that I am having trouble
with:

    
    
    __author__ = 'peter'
    
    
    
    from datetime import datetime, timedelta
    import csv
    
    
    TICKER='CHTR'
    STDEV_FILE = TICKER + '_stdev.csv'
    TRADES_FILE = TICKER + '_trades.csv'
    DATETIME_CSV_FORMAT = '%Y%m%d %H:%M:%S'
    
    def read_data(csv_filename):
      result = {}
      with open(csv_filename, 'rb') as csvfile:
         reader = csv.reader(csvfile, delimiter=',', quotechar='|')
         header = reader.next()
         #print 'HEADER',header
         for row in reader:
            new_data=None
            if len(row)==6: # this is a QUOTES file
              ticker, date, time, price, rtn, standard = row
              rtn = float(rtn)
              standard = float(standard)
              new_data = [rtn, standard]
            else: # assume this is a TRADES file otherwise
              ticker, date, time, price, size = row
              price = float(price)
              size = int(size)
              new_data = [price, size]
    
            date_object = datetime.strptime(date +' '+time, DATETIME_CSV_FORMAT)
            if not ticker in result:
              result[ticker]=[]
            result[ticker].append([date_object] + new_data)
      return result
    
    vol = read_data(STDEV_FILE)
    trades = read_data(TRADES_FILE
    

)

When I run it this is the error I recieve:

    
    
    Traceback (most recent call last):
      File "/home/peter/PycharmProjects/Vol/Vol.py", line 39, in <module>
        vol = read_data(STDEV_FILE)
      File "/home/peter/PycharmProjects/Vol/Vol.py", line 25, in read_data
        standard = float(standard)
    ValueError: could not convert string to float: #DIV/0!
    

I have run this on other csv files and had no trouble.

Here is a small sample of the csv file:

    
    
    SYMBOL  DATE    TIME    PRICE          RTN                     STDEV
    -----------------------------------------------------------------------
    CHTR    20130718    9:30:00 124.66  0                0
    -----------------------------------------------------------------------
    CHTR    20130718    9:30:00 124.66  0                0.0005674559
    -----------------------------------------------------------------------
    CHTR    20130718    9:30:00 124.56  -0.0008025039        0.0004539101
    -----------------------------------------------------------------------
    CHTR    20130718    9:30:00 124.54  -0.0001605781        0.0001135459
    -----------------------------------------------------------------------
    CHTR    20130718    9:30:00 124.54  0                0.0070177722
    -----------------------------------------------------------------------
    CHTR    20130718    9:31:56 123.310 -0.0099246286        0.011065531
    -----------------------------------------------------------------------
    CHTR    20130718    9:34:05 124.018 0.0057243955         0.0040363557
    -----------------------------------------------------------------------
    

Ultimately I would like to plot the standard deviation on y axis and seconds
from midnight on x axis. Any help would be appreciated.


Answer: The issue is that you are trying to convert the string "#DIV/0' to a float.
Obviously this is not possible. The cause of this is attempting to divide by
zero somewhere in your excel/csv sheet. Look back through your excel sheet and
see if anything is divided by zero and change it.

Alternately you could implement a simple if statement:

    
    
    if standard == '#DIV/0':
        print 'Error'
    else:
        standard = float(standard)
    

EDIT: Some references: <http://office.microsoft.com/en-us/excel-help/correct-
a-div-0-error-HP010066245.aspx>

