简体   繁体   English

在Flask中读取CSV文件并通过Jinga2进行迭代

[英]Reading a CSV file in Flask and iterating through Jinga2

I am trying to display data on my web app from a CSV file using Flask. 我正在尝试使用Flask从CSV文件在Web应用程序上显示数据。 The below code reads my CSV file and assigns stocklist as the variable for my data. 以下代码读取了我的CSV文件,并将库存清单分配为我的数据变量。 In the HTML code below it, using jinga logic, I iterate through stocklist however my CSV columns are returned as rows (see sample output and pic). 在下面的HTML代码中,使用jinga逻辑,我遍历了库存清单,但是我的CSV列作为行返回(请参见示例输出和图片)。 How do I display the rows correctly? 如何正确显示行?

My python function: 我的python函数:

@app.route('/stocks')
def Stocks():
    filename = 'stock_scraper - dev.csv'
    data = pandas.read_csv(filename, header=0)
    stocklist = list(data.values.flatten())
    return render_template('stocks.html', stocklist=stocklist)

My web app for iterating through stocklist: 我的用于遍历库存清单的Web应用程序:

            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>#</th>
                  <th>Ticker</th>
                  <th>Closing Price</th>
                  <th>Closing Date</th>
                </tr>
              </thead>
              <tbody>
                {% for eachstocks in stocklist%}
                <tr>
                  <td>{{ eachstocks }}</td>
                  <td>{{ eachstocks }}</td>
                  <td>{{ eachstocks }}</td>
                  <td>{{ eachstocks }}</td>
                </tr>
                {% endfor %}
              </tbody>
            </table>

Output: 输出: 表

You shouldn't flatten the list. 您不应该将列表弄平。

Try this: 尝试这个:

@app.route('/stocks')
def Stocks():
    filename = 'stock_scraper - dev.csv'
    data = pandas.read_csv(filename, header=0)
    stocklist = list(data.values)
    return render_template('stocks.html', stocklist=stocklist)

Then for the Jinja template: 然后对于Jinja模板:

            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>#</th>
                  <th>Ticker</th>
                  <th>Closing Price</th>
                  <th>Closing Date</th>
                </tr>
              </thead>
              <tbody>
                {% for value in stocklist%}
                <tr>
                  <td>{{ value[0] }}</td>
                  <td>{{ value[1] }}</td>
                  <td>{{ value[2] }}</td>
                  <td>{{ value[3] }}</td>
                </tr>
                {% endfor %}
              </tbody>
            </table>

Haks, i removed the nested loop and added the list position in each value to fix it. 哈克斯,我删除了嵌套循环,并在每个值中添加了列表位置以对其进行修复。 Works now. 现在可以使用。

          <tbody>
            {% for value in stocklist %}
                <tr>
                  <td>{{ value[0] }}</td>
                  <td>{{ value[1] }}</td>
                  <td>{{ value[2] }}</td>
                  <td>{{ value[3] }}</td>
                </tr>
            {% endfor %}
          </tbody> 

output enter image description here 输出在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM