获取以下代码的类型错误

时间:2014-10-02 10:38:13

标签: python

我收到以下代码的类型错误。代码的输出应该是此页面上的图形。http://www.realclearpolitics.com/epolls/other/president_obama_job_approval-1044.html。当我运行代码时,错误显示在代码的这一部分中。

reduce_the_data = new_take_page[new_colors.keys()].sum(axis=1)/100

def get_poll_data(poll_id):
    url = "http://charts.realclearpolitics.com/charts/%i.xml" %int(poll_id)
    return requests.get(url).text # is used to get the text from a url

def color_function(xml):
    dom = web.Element(xml)
    colors_dict ={}
    for i in dom.by_tag('graph'):
        name = i.attributes['title']
        hex_colors = i.attributes['color']
        colors_dict[name] = hex_colors 
    return colors_dict

def strip(s):
    re.sub(r'[\W_]+', '', s) 

def take_page(xml): 
    dom = web.Element(xml)
    final = {}

    charts_page = dom.by_tag('series')[0]    
    y = {i.attributes['xid']: str(i.content) for i in charts_page.by_tag('value')}

    key_of_y = y.keys()

    final['date'] = pd.to_datetime([y[j] for j in key_of_y])

    for each_value in dom.by_tag('graph'):
        title_name = each_value.attributes['title']
        new_dict = {n.attributes['xid']: float(n.content) 
                if n.content else np.nan for n in each_value.by_tag('value')}
        final[title_name] = [new_dict[k] for k in key_of_y]

    finals = pd.DataFrame(final)    
    finals = finals.sort(columns=['date'])

    return finals

def new_func(poll_id):
    new_poll_id = get_poll_data(poll_id)
    new_take_page= take_page(new_poll_id)
    new_colors = color_function(new_poll_id)

    new_take_page = new_take_page.rename(columns = {c: strip(c) for c in new_take_page.columns})

    reduce_the_data = new_take_page[new_colors.keys()].sum(axis=1)/100
    for x in new_colors.items():
        new_take_page[x] /= reduce_the_data

    for sticker, color in new_colors.items():
        plt.plot(new_take_page.date, new_take_page[sticker], color = color, label=  sticker)

    plt.xticks(rotation= 60)
    plt.legend(loc ='best')
    plt.xlabel("Date")
    plt.ylabel("Normalized Poll percentage")

>> new_func(1044)
>> plt.title("Polling")
TypeError: 'NoneType' object is not iterable

0 个答案:

没有答案