Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我很难理解为什么在Python中会出现这种类型错误_Python_Pygal - Fatal编程技术网

我很难理解为什么在Python中会出现这种类型错误

我很难理解为什么在Python中会出现这种类型错误,python,pygal,Python,Pygal,错误: 当我的结果在Python代码中生成完美浮动时: TypeError: unsupported operand type(s) for +: 'int' and 'str' 而且你在做同样的事情很多次;我指的是data=“,”。join(str(x)表示x内部索引数据)和print“,”。join(str(x)表示x内部索引数据) 您只需打印数据并删除打印“,”。加入(str(x)表示x内部索引数据) 您的代码大致如下所示: data = ",".join(str(x) for x i

错误:

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

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

而且你在做同样的事情很多次;我指的是
data=“,”。join(str(x)表示x内部索引数据)
print“,”。join(str(x)表示x内部索引数据)

您只需打印
数据
并删除
打印“,”。加入(str(x)表示x内部索引数据)

您的代码大致如下所示:

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)

看起来这就是问题所在:

@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)
house\u index\u data
应为a,您应拨打:

house_index_data = [x.encode('ascii') for x in spreadsheet.column[48]]
所以也许:

bar_chart.add('House Index', house_index_data)

这是可行的,但我使用的特定库需要浮点或IntIt,在我看来,您创建的变量
data
是一个字符串,使用它时,在
bar\u char.add(…,[data])
中,它应该是一个浮点列表。您是否应该调用
bar\u char.add(…,house\u index\u data)
?由于您自己的代码中没有任何地方使用
+
运算符,因此引发的
TypeError
的确切位置在哪里。如果您可以知道错误是在哪里引发的,它将帮助试图帮助您的人。但是,我认为
data
应该是一个float或int的列表,事实并非如此。感谢您的评论,但是纯格式的“house_index_data”会生成以下内容:['0.2566'、'0.522'…]等,这不是这个lib的正确格式,这就是为什么我建议使用:
float(x.encode('ascii'))
能否更新问题以包含堆栈跟踪?这应该证实了推理的路线。
house_index_data = [float(x.encode('ascii')) for x in spreadsheet.column[48]]
...
...
bar_chart.add('House Index', house_index_data)