gpt4 book ai didi

python - 将 csv 文件读取到 python ValueError : could not convert string to float

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:36 24 4
gpt4 key购买 nike

如有任何帮助,我们将不胜感激。请记住,我是 Python 的初学者。这是我遇到问题的代码部分:

__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

)

当我运行它时,这是我收到的错误:

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!

我已经在其他 csv 文件上运行过,没有遇到任何问题。

这是 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
-----------------------------------------------------------------------

最终我想在 y 轴上绘制标准偏差,在 x 轴上绘制从午夜算起的秒数。任何帮助将不胜感激。

最佳答案

问题是您正在尝试将字符串“#DIV/0”转换为 float 。显然这是不可能的。原因是您试图在 excel/csv 工作表中的某处除以零。回头看看通过你的 excel 表,看看是否有任何东西被零除并改变它。

或者,您可以实现一个简单的 if 语句:

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

编辑:一些引用资料:http://office.microsoft.com/en-us/excel-help/correct-a-div-0-error-HP010066245.aspx

关于python - 将 csv 文件读取到 python ValueError : could not convert string to float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24312690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com