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

时间:2018-08-31 20:49:45

标签: python pandas flask

如果我运行此代码,它将正确分页,而不会出现任何错误 builtins.IndexError IndexError:单个位置索引器越界“错误,我完全理解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

1 个答案:

答案 0 :(得分:0)

这部分代码似乎是唯一具有大熊猫相关代码并由于random.randint(1, 999)而引起随机行为的部分。文件 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]