试图绘制股票数据 - TypeError:' NoneType'对象不可调用

时间:2016-03-10 08:45:08

标签: python numpy matplotlib urllib

以下是代码:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import urllib

import numpy as np


def bytespdate2num(fmt, encoding='utf-8'):
    str_converter = mdates.strpdate2num(fmt)

    def bytes_converter(b):
        s = b.decode(encoding)
        return str_converter(s)

    return bytes_converter  

def graph_data(stock):
    print('Currently pulling:', stock)
    url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
    source_code=urllib.request.urlopen(url).read().decode()
    stock_data= []
    split_source= source_code.split('\n')

    for each_line in split_source:
        split_line=each_line.split(',')
        if len(split_line) == 6:
            if 'values' not in each_line:
                stock_data.append(each_line)

    data, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,delimiter=',', unpack= True, converters={0: bytespdate2num('%Y%m%d')})
    print(date)

stock = input('Stock to plot: ')
graph_data(stock)

这是错误:

Stock to plot: tsla
Currently pulling: tsla
Traceback (most recent call last):
  File "C:/Users/Welcome/Desktop/Learn/Mick.py", line 33, in <module>
    graph_data(stock)
  File "C:/Users/Welcome/Desktop/Learn/Mick.py", line 29, in graph_data
    data, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,delimiter=',', unpack= True, converters={0: bytespdate2num('%Y%m%d')})
  File "C:\Users\Welcome\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 928, in loadtxt
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "C:\Users\Welcome\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 928, in <listcomp>
    items = [conv(val) for (conv, val) in zip(converters, vals)]
TypeError: 'NoneType' object is not callable

我错过了什么?

0 个答案:

没有答案