![](/img/trans.png)
[英]Python, Flask - TypeError: string index indices must be integers or slices, not str
[英]Python Flask TypeError: string indices must be integers
Using Python Flask I want to take data from an Excel file and pass the content of each column to the html and use it to create a graph using chart.js, but the error I am facing is string indices must be integers.
@app.route('/data',methods = ['GET','POST']) def data(): if request.method == 'POST': file = request.form['upload-file']enter code here data=pd.read_excel(file) labels=list(file['student']) values=list(file['result']) return render_template('graph.html',labels=labels,values=values)
這是我發送數據的 HTML 文件的一部分
<script> const ctx = document.getElementById('myChart').getContext('2d'); const myChart = new Chart(ctx, { type: 'line', data: { labels: {{labels | safe }}, datasets: [{ label: '# of Votes', data: {{ values | safe }},
這就是我的 Excel 文件的樣子:
student result 0.2 14 0.3 15 0.4 16 0.5 19
該錯誤是由於嘗試使用 [] 對文件 object 進行切片而引起的。 這是不可能的。 您可能正在嘗試訪問 pandas dataframe。 不幸的是,您忘記添加正確的變量。 所以我會改變
file['student']
至:
data['student']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.