我正在与Pandas和Flask合作开发一个项目,我遇到了这个奇怪的错误,如下所示:IndexError:单个位置索引器超出范围。然而,错误本身并不奇怪,奇怪的是它是如何发生的。我有一个烧瓶应用程序,其中一个页面是一个进行ajax调用的表单。用户输入书名并返回该书的平均评级。如果我输入'It',那么评级会返回。一切正常。但是,如果我输入火车上的女孩,我会收到以下错误:
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
但是,如果我在我的熊猫代码中输入相同的标题,我就会得到评分!我知道我的Pandas代码运行正常。我还会说,任何有一个单词标题的书,如It,都能正常工作。标题中只有多个单词才会出现错误。我已将此问题追溯到此但无法继续进行。现在我的代码:
HTML代码
<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调用工作正常。只是当数据中有多个单词时,一切都崩溃了,我得到了错误信息。任何帮助都会很棒!谢谢!
答案 0 :(得分:0)
经过多次审核后发现这是一个正则表达式问题。请参阅上面的评论。在这个例子中,我的问题来自.title()方法,该方法改变了在CSV文件中搜索标题的方式。