This question already has answers here:
NLTK python error: “TypeError: 'dict_keys' object is not subscriptable”
(5个答案)
两年前关闭。
我试着在这里学习这个教程
https://www.codementor.io/jadianes/building-a-web-service-with-apache-spark-flask-example-app-part2-du1083854
使用spark,flask,我试着在win 10中使用带有这行代码的cmd发送curl的文件user_ratings.file
curl --data-binary @user_ratings.file http://127.0.0.1:5432/0/ratings

文件没有格式,只有此数据
260,9 1,8 16,7 25,8 32,9 335,4 379,3 296,7 858,10 50,8
post的功能是
@main.route("/<int:user_id>/ratings", methods=["POST"])
def add_ratings(user_id):
    # get the ratings from the Flask POST request object
    ratings_list = request.form.keys()[0].strip().split("\n")
    ratings_list = map(lambda x: x.split(","), ratings_list)
    # create a list with the format required by the negine (user_id, movie_id, rating)
    ratings = map(lambda x: (user_id, int(x[0]), float(x[1])), ratings_list)
    # add them to the model using then engine API
    recommendation_engine.add_ratings(ratings)

    return json.dumps(ratings)

但它不会在csv文件中发送任何瘦文件
spark消息错误为
 File "C:\Users\ibrahim\Desktop\RSS\app.py", line 33, in add_ratings
    ratings_list = request.form.keys()[0].strip().split("\n")
TypeError: 'dict_keyiterator' object is not subscriptable
127.0.0.1 - - [26/Sep/2017:23:46:48 +0200] "POST /0/ratings HTTP/1.1" 500 291 "-" "curl/7.55.1"

我觉得这行在函数中
 ratings_list = request.form.keys()[0].strip().split("\n")

在教程中,它将数据发布到csv文件
任何帮助,谢谢大家

最佳答案

您使用的是Python3,而教程使用的是Python2。您可以尝试:

ratings_list = list(request.form.keys())[0].strip().split("\n")

关于python - TypeError:“dict_keyiterator”对象不可下标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46437095/

10-16 00:56