【问题标题】:Reading a CSV file in Flask and iterating through Jinga2在 Flask 中读取 CSV 文件并遍历 Jinga2
【发布时间】:2019-02-24 17:23:07
【问题描述】:

我正在尝试使用 Flask 在我的 Web 应用程序上显示来自 CSV 文件的数据。下面的代码读取我的 CSV 文件并将 stocklist 分配为我的数据的变量。在它下面的 HTML 代码中,使用 jinga 逻辑,我遍历 stocklist 但是我的 CSV 列作为行返回(参见示例输出和图片)。如何正确显示行?

我的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)

我的用于遍历库存列表的网络应用:

            <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>

输出:

【问题讨论】:

    标签: python python-3.x flask jinja2


    【解决方案1】:

    Haks,我删除了嵌套循环并在每个值中添加了列表位置来修复它。现在可以使用了。

              <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> 
    

    输出 enter image description here

    【讨论】:

      【解决方案2】:

      您不应该展平列表。

      试试这个:

      @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)
      

      那么对于 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>
      

      【讨论】:

      • 我得到了 16 列。见下文。 # 收盘价 收盘日期 1 1 1 1 DODBX DODBX DODBX DODBX 100.69 100.69 100.69 100.69 02/22/19 02/22/19 02/22/19 02/22/19
      • 很高兴看到您能够修复它。我现在将编辑解决方案。
      猜你喜欢
      • 2021-08-15
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2016-10-30
      • 2019-09-29
      • 1970-01-01
      相关资源
      最近更新 更多