2017-01-27 137 views
0

我查看了各種示例,但出現錯誤。我正在使用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: 
+0

[相關](http://stackoverflow.com/questions/8420143/valueerror -could-not-convert-string-to-float-id#8420179) – Himal

回答

1

這一塊不明白它是什麼讓一個錯誤,但它刪除卷列的最後一個字符:

x = list(l[len(l)-1]) 
x = x[0:len(x)-1] 
x = ''.join(x) 
l[len(l)-1]=x 

有是與下列內容一致的線:

1485450601,828.5500,828.5500,828.4400,828.4999,0 

但正如我前面提到的,這會消除卷列中的最後一個字符;換句話說,將'0'轉換爲'',當轉換爲浮點時會生成錯誤。

另外行的最後終點必須予以消除,爲此,我們使用strip()

完整代碼:

from urllib.request import urlopen 

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').strip()).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 
     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)) 
    return timestamp_list, close_list, high_list, low_list, open_price_list, volume_list 
+0

夢幻般的效果很棒。謝謝。如果我想下載或做標準普爾500指數中的所有股票,併爲它們中的每一個做一個循環。爲了完成同樣的事情,我將如何去做它 – JourneyMan

+0

我不明白你的問題,解釋。 – eyllanesc

+0

目前上面的代碼將只能讀取或下載單個股票實時分鐘數據。我想知道如何下載標準普爾500指數上市股票的所有分鐘數據(或者可能只有100家開始,通常他們是500家標準普爾500名單上的公司名稱) – JourneyMan

相關問題