为什么python处理JSON会给我这个错误?

时间:2016-06-22 16:19:28

标签: python json matplotlib

所以代码尝试做的是从这个URL中提取数据,这是一个JSON对象。我从网址获取的数据是x-ceny-cen坐标。我将coords放在各自的列表中,以便以后绘图。获取信息的一切都很顺利。但是,它给了我以下错误:

  

" TypeError:ufunc' isfinite'输入类型不支持,并且输入无法根据投射规则安全地强制转换为任何支持的类型''''"。

我该如何解决这个问题?

    from urllib.request import urlopen
    import json
    import matplotlib.cm as cm
    import matplotlib.pyplot as plt
    from matplotlib.patches import Circle, PathPatch
    from matplotlib.path import Path
    from matplotlib.transforms import Affine2D
    import numpy as np
    import pylab
    import re

    dates = []
    xcen = []
    ycen = []
    sciObj = []
    def getNumberOfEntries(theJSON):
        return len(dates)   

    def getInfo(counter, theJSON):
        cont = True
        while cont:
    try:
        dates.append(theJSON["Events"][counter]["date"])
        xcen.append(theJSON["Events"][counter]["xCen"])
        ycen.append(theJSON["Events"][counter]["yCen"])
        sciObj.append(theJSON["Events"][counter]["sciObjectives"])
        counter = counter + 1
        getInfo(counter, theJSON)
    except IndexError:
        cont = False
    break

    def setXMax(theJSON):      
        xmax = xcen[0]
        for i in range (1, getNumberOfEntries(theJSON)-1):
            if xcen[i] > xmax:
        xmax = xcen[i]
        return int(xmax+10)

    def setXMin(theJSON):
        xmin = xcen[0]
        for i in range (1, getNumberOfEntries(theJSON)-1):
            if xcen[i] < xmin:
        xmin = xcen[i]
        return int(xmin-10)

    def setYMax(theJSON):      
        ymax = ycen[0]
        for i in range (1, getNumberOfEntries(theJSON)-1):
            if ycen[i] > ymax:
                ymax = ycen[i]
        return int(ymax+10)

    def setYMin(theJSON):
        ymin = ycen[0]
        for i in range (1, getNumberOfEntries(theJSON)-1):
            if ycen[i] < ymin:
                ymin = ycen[i]
        return int(ymin-10)         

    def plot():

        print('It got to here')

        fig, ax = plt.subplots(figsize=(25, 15))
        circle = Circle((0, 0), 980, facecolor='none', edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5)
ax.add_patch(circle)

plt.plot(xcen, ycen, 'ro', color = 'blue')
plt.plot(xcen, ycen, color = 'red')

pylab.xlim([setXMin, setXMax])
pylab.ylim([setYMin, setYMax])

for xy in zip(xcen, ycen):
    ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') 

ax.set_xticks(np.arange(setXMin, setXMax))
ax.set_yticks(np.arange(setYMin, setYMax))

plt.grid()
plt.show()

    def main():
        urlData = "http://www.lmsal.com/hek/hcr?cmd=search-events3&outputformat=json&instrument=IRIS&noaanum=11856&hasData=true"
        webUrl = urlopen(urlData)
        counter = 0
        if (webUrl.getcode()==200):                
            data = webUrl.read().decode('utf-8')
            theJSON = json.loads(data)
            getInfo(counter, theJSON)
        else:
            print ("You done messed up!!!")

        for i in range (0, getNumberOfEntries(theJSON)):
            print(dates[i])
            print("(", xcen[i], ", ", ycen[i], ")")
            print(sciObj[i])
            print(' ')

    main()
    plot()

0 个答案:

没有答案