Yahoo API ValueError: could not convert string to float:
Yahoo API ValueError: could not convert string to float:
我看过各种例子,但我有错误。我正在使用 python 3.5.2
我正在尝试使用 Yahoo chartapi 下载雅虎分钟数据 -- API 以及下面的 URL。
我得到
ValueError:无法将字符串转换为浮点数:
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:
这块不明白是干什么用的,但是删除了volume列的最后一个字符:
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
我看过各种例子,但我有错误。我正在使用 python 3.5.2 我正在尝试使用 Yahoo chartapi 下载雅虎分钟数据 -- API 以及下面的 URL。
我得到
ValueError:无法将字符串转换为浮点数:
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:
这块不明白是干什么用的,但是删除了volume列的最后一个字符:
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