简体   繁体   中英

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?

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) . File yerler.xsls probably doesn't have enough data and yeniYer = yer.iloc[rastgele] fails with that 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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