我查看了各種示例,但出現錯誤。我正在使用python 3.5.2 我正在嘗試使用雅虎chartapi - 下面流動的URL下載雅虎分鐘數據。Yahoo API ValueError:無法將字符串轉換爲浮點數:
我越來越
ValueError異常:無法將字符串轉換爲float:
def read_data(passing_for_url,fp):
all_features = []
timestamp_list =[]
close_list = []
high_list = []
low_list = []
open_price_list =[]
volume_list = []
count=0
if passing_for_url==1:
datasetname= (urlopen('http://chartapi.finance.yahoo.com/instrument/1.0/GOOG/chartdata;type=quote;range=1d/csv').read().decode('utf-8')).split('\n')
else:
datasetname = fp
for line in datasetname:
l=line.split(',')
#print (l)
if(passing_for_url==1):
if count > 16:
fp.write(line)
else:
count+=1
continue
x = list(l[len(l)-1])
x = x[0:len(x)-1]
x = ''.join(x)
l[len(l)-1]=x
print (l)
all_features.append(l)
timestamp, close, high, low, open_price , volume = l
timestamp_list.append(int(timestamp))
close_list.append(float(close))
high_list.append(float(high))
low_list.append(float(low))
open_price_list.append(float(open_price))
volume_list.append(float(volume)) # <== Getting error here
return timestamp_list, close_list, high_list, low_list, open_price_list, volume_list
下面是從URL
uri:/instrument/1.0/GOOG/chartdata;type=quote;range=1d/csv
ticker:goog
Company-Name:Alphabet Inc.
Exchange-Name:NMS
unit:MIN
timezone:EST
currency:USD
gmtoffset:-18000
previous_close:835.6700
Timestamp:1485441000,1485464400
labels:1485442800,1485446400,1485450000,1485453600,1485457200,1485460800,1485464400
values:Timestamp,close,high,low,open,volume
close:827.1602,833.9300
high:827.4200,834.6201
low:827.0100,833.9300
open:827.3400,833.9300
volume:0,99800
1485441610,833.9300,833.9300,833.9300,833.9300,99800 <== Need to start here
1485442196,831.0830,831.0830,831.0830,831.0830,47700
1485442503,832.3000,832.3000,832.3000,832.3000,60800
1485442811,832.2100,832.2100,832.2100,832.2100,33000
1485443111,831.4300,831.4300,831.4300,831.4300,41900
1485443408,831.0120,831.0120,831.0120,831.0120,34600
1485443712,831.8400,831.8400,831.8400,831.8400,39600
1485443997,832.3400,832.3400,832.3400,832.3400,38400
1485444312,831.7600,831.7600,831.7600,831.7600,36000
1485444579,831.0001,831.4000,831.0000,831.4000,94700
我只需要具有從數據的響應樣本時間戳,關閉,高,低,open_price,音量及以下,前17行被省略。
但我使用Python 3.5.2
ValueError: could not convert string to float:
Traceback (most recent call last):
File "google.py", line 207, in <module>
timestamp_list, close_list, high_list, low_list, open_price_list, volume_list = read_data(choice, fp1)
File "google.py", line 49, in read_data
volume_list.append(float(volume))
ValueError: could not convert string to float:
[相關](http://stackoverflow.com/questions/8420143/valueerror -could-not-convert-string-to-float-id#8420179) – Himal