【问题标题】:My results are truncating into a single row. How do I get it to print everything?我的结果被截断为一行。我如何让它打印所有内容?
【发布时间】:2023-03-23 14:15:02
【问题描述】:

这是我的代码: '

导入pyodbc 将熊猫导入为 pd from flask import Flask, request, render_template

app = Flask(__name__)


@app.route('/')
def attendance_fetch():
    return render_template('Attendance_fetch.html')


@app.route('/Attendance', methods=['GET', 'POST'])
def database_file():
    df = 0
    if request.method == 'POST':

        employee = request.form['employee_code']

        conn = pyodbc.connect(
            r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=file_location;')
        cursor = conn.cursor()
        cursor.execute('select * from Employees where EmployeeCode =?', employee)

        for i in cursor.fetchall():
            employee_id = str(i[0])
            cursor.execute('select EmployeeID, AttendanceDate, InTime, OutTime, TotalDurationinHHMM'
                           ' from AttendanceLogs where EmployeeId=?', employee_id)
            columns = [column[0] for column in cursor.description]
            print(columns)
            results = []
            for row in cursor.fetchall():
                results.append(dict(zip(columns, row)))
            for result in results:
                final = [result]
                df = pd.DataFrame(final)

            return render_template('Testrun2.html', tables=[df.to_html(classes='data')], 
            titles=df.columns.values)

        cursor.close()
        conn.close()


if __name__ == '__main__':
    app.run(debug=True)

'

然后我写了一个html文件来捕捉它:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Attendance Table</title>
</head>
<body>

{% for table in tables %}
            {{titles[loop.index]}}
            {{ table|safe }}
{% endfor %}
</body>
</html>

But the result is just a singular truncated column: 我希望打印每一行结果。提前致谢。

【问题讨论】:

  • 您的问题过于笼统和模糊,请阅读guide lines,了解如何提出好的问题以增加获得答案的机会。

标签: python pandas flask pyodbc


【解决方案1】:

问题出在:

 for result in results:
            final = [result]
            df = pd.DataFrame(final)

您在每个循环中都覆盖了您的 df,其中包含一个结果。将 df 放在循环之外并尝试它是否有效。

【讨论】:

  • 非常感谢。我是一个完全的业余爱好者,一直在努力理解出了什么问题。 'df = pd.DataFrame(results)' 几乎解决了它。
猜你喜欢
  • 2014-05-22
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2020-11-17
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多