[英]Reading csv file to python ValueError: could not convert string to float
Any help with this would be appreciated. 任何帮助都将不胜感激。 Please keep in mind that I am a beginner in Python.
请记住,我是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. 我在其他csv文件上运行此操作并没有遇到任何问题。
Here is a small sample of the csv file: 以下是csv文件的一小部分示例:
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. 最后,我想绘制y轴上的标准偏差和x轴上午夜的秒数。 Any help would be appreciated.
任何帮助,将不胜感激。
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. 问题是你正在尝试将字符串“#DIV / 0”转换为浮点数。显然这是不可能的。原因是你试图在excel / csv表单中的某处除以零。回顾你的excel表单并查看是否有任何内容除以零并更改它。
Alternately you could implement a simple if statement: 或者你可以实现一个简单的if语句:
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 编辑:一些参考: http : //office.microsoft.com/en-us/excel-help/correct-a-div-0-error-HP010066245.aspx
Instead of "if standard == '#DIV/0':", it might be easier and more readable to do: 而不是“if standard =='#DIV / 0':”,它可能更容易,更易读:
if standard.isdigit():
standard = float(standard)
else:
# error code
str.isdigit() will check all the characters in 'str' to see if they are digits. str.isdigit()将检查'str'中的所有字符,看它们是否为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.