ฉันกำลังทำงานกับ Pandas และ Flask และฉันได้รับข้อผิดพลาดแปลก ๆ ซึ่งมีดังต่อไปนี้: IndexError: ตัวสร้างดัชนีตำแหน่งเดียวอยู่นอกขอบเขต อย่างไรก็ตาม ข้อผิดพลาดไม่ใช่สิ่งที่แปลก แต่สิ่งที่แปลกคือมันเกิดขึ้นได้อย่างไร ฉันมีแอปขวดที่มีหน้าใดหน้าหนึ่งเป็นแบบฟอร์มที่ทำการโทรอาแจ็กซ์ ผู้ใช้ป้อนชื่อหนังสือและส่งคืนคะแนนเฉลี่ยของหนังสือเล่มนั้น ถ้าฉันพิมพ์คำว่า 'It' คะแนนจะกลับมา ทุกอย่างทำงานได้ดี อย่างไรก็ตาม ถ้าฉันพิมพ์ The Girl on the Train ฉันได้รับข้อผิดพลาดต่อไปนี้:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/mikecuddy/Desktop/Coding/Python/book_ratings/app.py", line 75, in book_look_up
rating = book.book_rating(title)
File "/Users/mikecuddy/Desktop/Coding/Python/book_ratings/books.py", line 17, in book_rating
rating = info.iloc[0][12]
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1296, in __getitem__
return self._getitem_axis(key, axis=0)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1612, in _getitem_axis
self._is_valid_integer(key, axis)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py", line 1526, in _is_valid_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
อย่างไรก็ตาม ถ้าฉันป้อนชื่อ SAME ลงในโค้ดแพนด้า ฉันจะได้รับคะแนน! ฉันรู้ว่ารหัส Pandas ของฉันทำงานได้ดี ฉันจะบอกด้วยว่าหนังสือเล่มใดก็ตามที่มีชื่อที่มีคำเดียว เช่น มัน ใช้ได้ดี มีเพียงคำหลายคำในชื่อเท่านั้นที่ข้อผิดพลาดของฉันเกิดขึ้น ฉันได้ติดตามปัญหานี้แล้ว แต่ไม่สามารถดำเนินการต่อไปได้ ตอนนี้รหัสของฉัน:
รหัสเอชทีเอ็มแอล
<form class='title_lookUp_div' id='book_look_up_form'>
<label>Book Title: </label><input id='title' name='title' type='text'>
<p>Result: <span id='result'>?</span></p>
<button>Submit</button>
</form>
JS/AJAX โทร:
$(document).ready(function(){
$('#book_look_up_form').bind('submit', function(event){
event.preventDefault();
$.ajax({
data: {
title: $('#title').val()
},
type: 'POST',
url: '/book_look_up',
success: function(data){
$('#result').text(data.result).show();
}
});
});
});
รหัสหมีแพนด้า:
def book_rating(self, title):
#Gettting the row of data for the title that the user entered.
info = self.__data[self.__data.original_title == title]
rating = info.iloc[0][12]
return rating
รหัส app.py:
@app.route('/book_look_up',methods=['POST'])
def book_look_up():
#Recieving the data from the ajax call
title = request.form['title']
#The data in the CSV file has the first letter of each word capitalized
title = title.title()
#Creating the object that will deal with the data from CSV file.
book = Books()
rating = book.book_rating(title)
if title:
return jsonify(result = rating)
return jsonify({'error' : 'Missing Data'})
อย่างที่ฉันบอกไปแล้วว่าโค้ด Pandas ทำงานได้ดี การเรียก app.py และ ajax ก็ทำงานได้ดี เมื่อข้อมูลมีหลายคำทุกอย่างก็ขัดข้องและฉันได้รับข้อความแสดงข้อผิดพลาดข้างต้น ความช่วยเหลือใด ๆ จะดีมาก! ขอบคุณ!
info
ในรหัสแพนด้าเมื่อขัดข้องคือเท่าใด - person Keith   schedule 29.09.2017