Reading csv file to python ValueError: could not convert string to float -



Reading csv file to python ValueError: could not convert string to float -

any help appreciated. please maintain in mind beginner in python. portion of code having problem with:

__author__ = 'peter' 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 = {} open(csv_filename, 'rb') csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') header = reader.next() #print 'header',header row in reader: new_data=none if len(row)==6: # quotes file ticker, date, time, price, rtn, standard = row rtn = float(rtn) standard = float(standard) new_data = [rtn, standard] else: # assume trades file otherwise ticker, date, time, price, size = row cost = 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) homecoming result vol = read_data(stdev_file) trades = read_data(trades_file

)

when run error recieve:

traceback (most recent phone 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: not convert string float: #div/0!

i have run on other csv files , had no trouble.

here little sample of csv file:

symbol date time cost 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 plot standard deviation on y axis , seconds midnight on x axis. help appreciated.

the issue trying convert string "#div/0' float. not possible. cause of attempting split 0 somewhere in excel/csv sheet. through excel sheet , see if divided 0 , alter it.

alternately implement simple if statement:

if standard == '#div/0': print 'error' else: standard = float(standard)

edit: references: http://office.microsoft.com/en-us/excel-help/correct-a-div-0-error-hp010066245.aspx

python csv

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -