2017-01-27 9 views
0

さまざまな例を見ましたが、エラーがあります。私はPython 3.5.2を使用しています 私はYahoo chartapiを使用してyahoo分のデータをダウンロードしようとしています - 下の流れるURLのAPI。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 

からの応答のサンプルです私はからのデータを持っているだけですタイムスタンプ、クローズ、ハイ、ロー、オープンプライス、ボリューム以下の場合、最初の17行は省略されます。

しかし、私はこの作品は、それが何のためにあるのか分かりませんが、それは、ボリュームの列の最後の文字を削除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

のpythonを使用して、エラーを取得しています:

あり
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'を ''に変換します。これはfloatに変換するとエラーが生成されます。行の最後端は、我々は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

それは素晴らしい作品です。ありがとうございました。もし私がS&P 500のリストをダウンロードしたり全ての株を売り出したいのであれば、それぞれのためのループを作ってください。同じことを達成するために、どうすればそれについて行きますか? – JourneyMan

+0

あなたの質問を理解できません、説明してください。 – eyllanesc

+0

現在、上記のコードでは、単一の在庫リアルタイム分データの読み取りまたはダウンロードのみが行われます。私はS&P 500のリストに記載されている株式のすべての分データをダウンロードする方法を考えていた(または、開始するのはわずか100人かもしれないが、通常S&P 500を構成するリストの約500社の企業名である) – JourneyMan

関連する問題