简体   繁体   English

Flask 有时会正常工作,有时会导致“builtins.IndexError”错误?

[英]Flask sometimes works properly and sometimes causes "builtins.IndexError" error?

If I run this code, it will be correctly paged without any error builtins.IndexError IndexError: single positional indexer out of bounds "error, I fully understand the intenettene resolution, do you help? When I look at the relevant articles, I do not understand the solution of some of them, but some do not understand it because they are not Turkish, and can you help me?如果我运行这段代码,它会被正确分页,没有任何错误builtins.IndexError IndexError: single positional indexer out of bounds “错误,我完全理解 intenettene 解析,你有帮助吗?当我看相关文章时,我没有理解其中一些的解决方案,但有些不明白,因为他们不是土耳其语,你能帮我吗?

from flask import Flask,render_template,flash,redirect,url_for,session,logging,request
from flask_mysqldb import MySQL
from wtforms import Form,StringField,TextAreaField,PasswordField,validators
from passlib.hash import sha256_crypt
from functools import wraps
import pandas as pd
import random
import time
import sys

app = Flask(__name__)
app.secret_key= "ybblog"

app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = ""
app.config["MYSQL_DB"] = "ybblog"
app.config["MYSQL_CURSORCLASS"] = "DictCursor"
mysql = MySQL(app)

@app.route("/")
def index():   
    return render_template("site.html")
@app.route("/maps")
def about():
    return render_template("maps.html")
#Atama Url
@app.route("/sonuc",methods=["GET","POST"])
def sonuc():

    cursor = mysql.connection.cursor()
    sorgu = "Select * From site"
    result = cursor.execute(sorgu)    
    if result >0:
        articles = cursor.fetchall()
        cursor.execute("DELETE FROM site")
        mysql.connection.commit()
        cursor.close()
        return render_template("sonuc.html",articles= articles)
    else:
        cursor.execute("DELETE FROM site")
        mysql.connection.commit()
        cursor.close()
        return render_template("site.html")
    cursor.execute("DELETE FROM site")
    mysql.connection.commit()
    cursor.close()



@app.route("/bilgi",methods=["GET","POST"])
def bilgi():    
    if request.method == "POST":
        yer = pd.read_excel("yerler.xlsx")
        keyword = request.form.get("keyword")
        isimler = keyword.split(",")
        time.sleep(1)
        for isim in isimler:
            rastgele = random.randint(1,999)
            yeniYer = yer.iloc[rastgele]

            cursor = mysql.connection.cursor()    
            sorgu = "Insert into site(yerler,bolge,teskilati,ACM,ili,isim) VALUES(%s,%s,%s,%s,%s,%s)"
            cursor.execute(sorgu,(yeniYer["yerler"],yeniYer["bolge"],yeniYer["teskilati"],yeniYer["ACM"],yeniYer["ili"],isim))
            mysql.connection.commit()

        cursor.close()

        flash("Başarılı...","success")
        return redirect(url_for("sonuc")) #sonuca yönlendir
    else:
        flash("Olmadı ...","success")
        return render_template("site.html")
if __name__ == "__main__":
    app.run(debug=True)

builtins.IndexError内置索引错误

Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python35\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python35\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python35\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Python35\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python35\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python35\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python35\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Python35\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python35\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\WeLoo\Desktop\YBBLOG\site2.py", line 60, in bilgi
    yeniYer = yer.iloc[rastgele]
  File "C:\Python35\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Python35\lib\site-packages\pandas\core\indexing.py", line 2102, in _getitem_axis
    self._validate_integer(key, axis)
  File "C:\Python35\lib\site-packages\pandas\core\indexing.py", line 2009, in _validate_integer
    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

This part of the code seems to be the only part that has pandas related code and induces random behaviour because of random.randint(1, 999) .这部分代码似乎是唯一具有 Pandas 相关代码并由于random.randint(1, 999)引起随机行为的部分。 File yerler.xsls probably doesn't have enough data and yeniYer = yer.iloc[rastgele] fails with that IndexError文件yerler.xsls可能没有足够的数据,并且yeniYer = yer.iloc[rastgele]IndexError失败

yer = pd.read_excel("yerler.xlsx")

keyword = request.form.get("keyword")
isimler = keyword.split(",")

for isim in isimler:
    rastgele = random.randint(1,999)
    yeniYer = yer.iloc[rastgele]

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

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