-1

错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

当我的结果在我的 Python 代码中生成完美的浮点数时:

data = ",".join(str(x) for x in house_index_data)

0.12,0.29,0.13,0.12,0.23,0.13,0.07,0.21,0.22,0.18,0.11,0.18,0.16,0.07,0.20,
0.11,0.09,0.11,0.18,0.07,0.12,0.14,0.14,0.11,0.21,0.21,0.06,0.20,0.16,0.00,
0.14,0.12,0.04,0.10,0.12,0.13,0.11,0.21,0.22,0.08,0.11,0.12,0.08,0.00,0.13,
0.05,0.14,0.12,0.09,0.12,0.14,0.15,0.09,0.14,0.07,0.07,0.00,0.41,0.01,0.08,
0.08,0.09,0.10,0.21,...

@app.route('/main')
@login_required
def main():
    # Data from .csv file
    ######################
    base_dir = "db/"
    logged_in_user = g.user
    spreadsheet = pe.get_sheet(file_name=os.path.join(base_dir, "example.csv"))
    house_index_data = [x.encode('ascii') for x in spreadsheet.column[48]]
    print ",".join(str(x) for x in house_index_data)
    # data = str(house_index_data).replace("'", "")
    data = ",".join(str(x) for x in house_index_data)

    # Graphs (Bar) data
    #####################
    bar_chart = pygal.StackedBar(fill=True, interpolate='cubic', style=BlueStyle)
    bar_chart.title = 'Stack bar chart of House Index'
    bar_chart.add('House Index',  [data])
    bar_chart.render()

    return render_template('main.html',
                           user=logged_in_user,
                           value=spreadsheet,
                           bar_chart=bar_chart,
                           line_chart=line_chart)
4

2 回答 2

0

参考

此外,您多次做同样的事情;我指的是data = ",".join(str(x) for x in house_index_data)print ",".join(str(x) for x in house_index_data)

您只需打印data并删除print ",".join(str(x) for x in house_index_data)

您的代码或多或少将如下所示:

@app.route('/main')
@login_required
def main():
    # Data from .csv file
    ######################
    base_dir = "db/"
    logged_in_user = g.user
    spreadsheet = pe.get_sheet(file_name=os.path.join(base_dir, "example.csv"))
    house_index_data = [x.encode('ascii') for x in spreadsheet.column[48]]
    # data = str(house_index_data).replace("'", "")
    data = ",".join(str(x) for x in house_index_data)
    print data

    # Graphs (Bar) data
    #####################
    bar_chart = pygal.StackedBar(fill=True, interpolate='cubic', style=BlueStyle)
    bar_chart.title = 'Stack bar chart of House Index'
    bar_chart.add('House Index',  [data])
    bar_chart.render()

    return render_template('main.html',
                           user=logged_in_user,
                           value=spreadsheet,
                           bar_chart=bar_chart,
                           line_chart=line_chart)
于 2017-08-24T14:48:15.850 回答
0

看起来这是个问题:

house_index_data = [x.encode('ascii') for x in spreadsheet.column[48]]

house_index_data应该是浮点数列表,您应该调用:

bar_chart.add('House Index', house_index_data)

所以也许:

house_index_data = [float(x.encode('ascii')) for x in spreadsheet.column[48]]
...
...
bar_chart.add('House Index', house_index_data)
于 2017-08-24T14:55:03.630 回答