2017-08-24 54 views
-1

錯誤:我有一個很難理解爲什麼我在Python得到這個類型錯誤

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) 
+0

這工作,但我使用的特定圖書館要求要麼浮動或詮釋 –

+0

它看起來對我來說,變量'data'你創建的是一個字符串,當它被使用時,在'bar_char.add(...,[data])'它應該是一個浮動列表。你應該調用'bar_char.add(...,house_index_data)'而不是? – quamrana

+2

因爲你沒有在你自己的代碼中使用'+'運算符,所以'TypeError'被引發。 – chepner

回答

0

看起來這是問題:

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

house_index_data應該是一個list of floats,你應該叫:

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) 
+0

感謝您的評論,但純粹的格式'house_index_data'產生這樣的:['0.2566','0.522'...]等,這是不正確的格式這個Lib –

+0

這就是爲什麼我建議:'float(x .encode('ascii'))' – quamrana

+0

你能否更新問題以包含堆棧跟蹤?這應該證實推理的路線。 – quamrana

0

Reference

而且你多次做同樣的事情;我指的是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) 
相關問題