【问题标题】:Unable to correct "TypeError: 'int' object is not iterable"无法更正“TypeError:'int' 对象不可迭代”
【发布时间】:2022-01-14 18:04:05
【问题描述】:

有python代码: ....

def DisplayTable(filename):
    #dir = os.path.dirname(__file__)
    fullFilename = dir+'/Data/'+str(filename)
    data = pd.read_csv(fullFilename, header=None)
    data=data.iloc[:: -1]
    print(data)
    stringList = [str(x) for x in data] 
    print(stringList)
    #data= tuple(data)
    headings=(" ", "Date", " ", "Temps")
    return render_template('fDetailTemps.html',  data=data, headings=headings)`

我的 HTML 给出了错误:TypeError: 'int' object is not iterable

我已经尝试了在这个板上找到的几个解决方案,但没有一个有帮助(最新的试用版在上面的代码中使用str(x)。) print(data) 声明给出:

4    Date:   2021-Dec-20, 04:00       Kombucha Temp:   77.56
3    Date:   2021-Dec-20, 03:00       Kombucha Temp:   77.79
2    Date:   2021-Dec-20, 02:00       Kombucha Temp:   77.79
1    Date:   2021-Dec-20, 01:00       Kombucha Temp:   77.90
0    Date:   2021-Dec-20, 00:00       Kombucha Temp:   77.79

print(string_list) 给出:

['0', '1', '2', '3']

部分 HTML 代码:

       <table>
        <tr>
           {% for header in headings %}
              <th>{{ header }} </th>
           {% endfor %}
        </tr>
        {% for row in data %}
           <tr>
               {% for cell in row %}
                  <td> {{ cell }} </td>
               {% endfor %}
           </tr>
        {% endfor %}
        </table>

【问题讨论】:

    标签: python html


    【解决方案1】:

    删除 HTML 中的 for cell in row

    for row in data 使 row 成为整数 0 或 1 或 2...等等,因此 for cell in row 没有意义,因为行是整数而不是要迭代的列表。

    【讨论】:

    • TY - 它确实消除了 int 错误消息,但是它根本没有显示任何数据。我应该删除整个 {% for cell in row %} {{ cell }} {% endfor %} ?我尝试删除和不删除 {{ cell }} 部分
    • 试试return render_template('fDetailTemps.html', data=data.iterrows(), headings=headings)
    • 和 HTML %for index,row in data%
    • 这将使row 成为index 的对应行,例如索引0 将具有以下行:Date: 2021-Dec-20, 00:00 Kombucha Temp: 77.79
    • 几乎?标题现在是正确的,但没有显示数据。这就是我现在在 HTML 中的内容: {% for header in headings %} {{ header }} {% endfor %} {% for index,row in data % } {{ 单元格 }} {% endfor %}
    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2018-09-29
    • 2020-02-26
    • 2015-04-06
    • 2013-10-31
    相关资源
    最近更新 更多